145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
1\2/3 思路: 借助于一个栈,依次将根节点的右子节点和左子节点压入栈中。如果一个节点为叶子节点,或者前一个出栈的元素为当前栈顶节点的子节点,则出栈。
vector<int> postorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> _stack;TreeNode *cur=root;TreeNode *pre=NULL;if(cur!=NULL)_stack.push(cur);while(!_stack.empty()){cur=_stack.top();if((cur->left==NULL&&cur->right==NULL)||(pre&&(cur->left==pre||cur->right==pre))){res.push_back(cur->val);pre=cur;_stack.pop();}else{if(cur->right)_stack.push(cur->right);if(cur->left)_stack.push(cur->left);}}return res;}
转载于:https://www.cnblogs.com/zhaoyaxing/p/8502683.html
145. Binary Tree Postorder Traversal相关推荐
- 【二叉树的迭代版后序遍历】LeetCode 145. Binary Tree Postorder Traversal
LeetCode 145. Binary Tree Postorder Traversal Solution1:递归版答案 二叉树的后序遍历递归版是很简单的,关键是迭代版的代码既难理解又难写!但听了花 ...
- LeetCode 145. Binary Tree Postorder Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-postorder-traversal/ 题目: Given a binary tree, retu ...
- LeetCode:145. Binary Tree Postorder Traversal
总共想出了四种方法来做. 第一种是递归,不说了.后面集中都是迭代. 第二种是使用一个栈,而且借助一个set来记录哪些node是访问过它的子节点的,防止重复访问.缺点是需要额外的set的空间 class ...
- 145. Binary Tree Postorder Traversal 二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1\2/3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? DFS 今天这道题比较简单,直 ...
- LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法
题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...
- 5. Binary Tree Postorder Traversal
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Binary Tree Postorder Traversal
https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return the posto ...
- Leetcode 之Binary Tree Postorder Traversal(44)
后序遍历,比先序和中序都要复杂.访问一个结点前,需要先判断其右孩子是否被访问过.如果是,则可以访问该结点:否则,需要先处理右子树. vector<int> postorderTravers ...
最新文章
- Chapter 2 Open Book——29
- 如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据
- STM32的串口通信
- java质数判断程序_java判断一个数是否为素数/质数
- python 进程池、线程池 与异步调用、回调机制
- oracle chinese_china.al32utf8,Oracle11g字符集更改为AL32UTF8
- Power BI for Office 365 概览
- dw怎么打开html模板,Dreamweaver怎样使用网页模板及修改模板
- DOS原理和常用命令详解示例
- windows10怎么卸载计算机,Win10怎么卸载声卡驱动?电脑声卡驱动怎么卸载?
- supermap地图展示OD图
- 开发一个APP系统软件需要多少钱及需要多长开发周期
- 7段数码管动态扫描c语言,7段数码管动态扫描原理及其实现方式
- IP地址-子网掩码-缺省网关
- 高频故障-桌面图标变成白纸图标的恢复方案
- 测试第一步,雀氏纸尿裤
- My设置密码报错:ERROR 1819 (HY000): Your password does not satisfy the current policy requir
- 不能在具有唯一索引 ‘NonClusteredIndex-20220419-093301‘ 的对象 ‘dbo.cux_machine_info‘ 中插入重复键的行。 语句已终止。
- 微信怎么收银行卡的消息服务器,你的银行卡短信通知收费吗
- 计算机网络经典选择题20道