Leetcode 之Binary Tree Postorder Traversal(44)

后序遍历,比先序和中序都要复杂。访问一个结点前,需要先判断其右孩子是否被访问过。如果是,则可以访问该结点;否则,需要先处理右子树。
vector<int> postorderTraversal(TreeNode *root)
{
vector<int> result;
stack<TreeNode *>s;
TreeNode *p, *q;//一个表示当前访问的结点,一个表示刚刚访问过的结点
p = root;
do
{
while (p != nullptr)
{
//不断将左结点压入
s.push(p);
p = p->left;
}
q = nullptr;//压到底时,刚刚访问过的结点必定为空结点
while (!s.empty())
{
p = s.top();
s.pop();
if (p->right == q)
{
//如果当前结点的右结点已经被访问,则访问该结点
result.push_back(p->val);
q = p;
}
else
{
//右孩子没有被访问过,则继续压入,先处理右子树
s.push(p);
p = p->right;
break;
}
}
} while (!s.empty());
}
Leetcode 之Binary Tree Postorder Traversal(44)的更多相关文章
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- leetcode - [6]Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【leetcode】Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
随机推荐
- [九省联考2018]IIIDX 贪心 线段树
~~~题面~~~ 题解: 一开始翻网上题解看了好久都没看懂,感觉很多人都讲得不太详细,所以导致一些细节的地方看不懂,所以这里就写详细一点吧,如果有不对的or不懂的可以发评论在下面. 首先有一个比较明显 ...
- HDU.1233 还是畅通工程(Prim)
HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...
- Nginx反向代理两个tomcat服务器
第一步,在Linux上安装两个tomcat,修改好端口号后,启动起来. 第二步,配置本地的DNS解析,即修改host文件: 第三步,配置Nginx配置文件 反向代理的配置虚拟主机配置差不多也要配置虚拟 ...
- SSO解决session共享的几种方案
之前做项目遇到了这个sso系统,当时只是理解了一部分,今天偶尔发现一篇文章,觉得写的不错,增加了sso知识: 单点登录在现在的系统架构中广泛存在,他将多个子系统的认证体系打通,实现了一个入口多处使用, ...
- [ethernet]ubuntu更换网卡驱动
问题: 网络不能ping通,dmesg显示很多 [::00.0: eth0: link up [::00.0: eth0: link up [::00.0: eth0: link up [::00.0 ...
- 008.C++类改写模板类
1.普通类 //class head class complex //class body {} { public: complex(, double i) :re(r), im(i) {}//构造函 ...
- wamp环境介绍
一.简介 Wamp就是 Windows Apache Mysql PHP集成安装环境,即在window下的apache.php和mysql的服务器软件. 二.常用的集成环境 XAMPP - XAMPP ...
- 解决方案:WindowsError: [Error 2]
使用Python的rename()函数重命名文件时出现问题,提示 WindowsError: [Error 2] 错误,最初代码如下: def renameFile(filename): filePr ...
- btrace 常见问题
执行btrace命令报错:Unable to open socket file: target process not responding or HotSpot VM not loaded ---- ...
- 【STSRM10】数学上来先打表
[算法]DP+数学计数 [题意]给出n个点(不同点之间有区别),求出满足下列条件的连边(双向边)方案(对1004535809取模): 1.每条边连接两个不同的点,每两个点之间至多有一条边. 2.不存在 ...