Leetcode#145 Binary Tree Postorder Traversal
递归写法谁都会,看看非递归写法。
对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子。所以,最直接的想法就是在栈中记录到底遍历了几个儿子。
代码:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> path;
stack<pair<TreeNode *, int> > st; st.push(pair<TreeNode *, int>(root, ));
while (!st.empty()) {
pair<TreeNode *, int> top = st.top();
st.pop(); if (!top.first)
continue; if (top.second == ) {
top.second = ;
st.push(top);
st.push(pair<TreeNode *, int>(top.first->left, ));
}
else if (top.second == ) {
top.second = ;
st.push(top);
st.push(pair<TreeNode *, int>(top.first->right, ));
}
else if (top.second == ) {
path.push_back(top.first->val);
}
} return path;
}
另外还有一种技巧性比较强的写法,只使用普通的栈就可以做到。利用到了一个后续遍历的特点,即:
后续遍历序列中,父节点前面一定紧挨着他的儿子节点(如果有的话)
所以,额外保存一个prev变量代表前一个遍历的节点,就可以识别出上面的情况。如果prev节点是当前节点的儿子节点(或者当前节点没有儿子),说明当前节点的儿子节点都遍历过了,可以遍历父节点了。
如何更新维护prev呢?当一个节点输出后,将prev置为这个节点,这是因为如果prev是后续遍历序列中的前一个节点,那么这个prev节点必须是已经被输出的节点。
代码如下:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> path;
stack<TreeNode *> st;
TreeNode *prev = NULL; st.push(root);
while (!st.empty()) {
TreeNode *node = st.top(); if (!node)
st.pop();
else if ((!node->left && !node->right)
|| (prev && (node->left == prev || node->right == prev))) {
path.push_back(node->val);
st.pop();
prev = node;
}
else {
st.push(node->right);
st.push(node->left);
}
} return path;
}
也就短了几行而已。。
Leetcode#145 Binary Tree Postorder Traversal的更多相关文章
- 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
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- 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 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- PHP文件上传错误类型及说明
从 PHP 4.2.0 开始,PHP 将随文件信息数组一起返回一个对应的错误代码.该代码可以在文件上传时生成的文件数组中的 error 字段中被找到,也就是 $_FILES['userfile'][' ...
- 复习URLHttpConnection方式GET,POST方式链接网络解析uri
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- scala学习资料
强烈推荐一个s在线学习scala的网站: http://zh.scala-tour.com/#/overview
- Mac上添加adb_usb.ini
max上添加android驱动支持 用到的命令: 命令方式最简单,键入如下两行命令你就可以实现对文件的现实和隐藏功能了.这个时候肯定会有童鞋问:“在哪里敲命令呢?”,Launchpad——其他——终端 ...
- sendBroadcast 无法接收
项目中遇到已经sendbroadcast,但是在对应的BroadcastReceiver中却无法调用onReceiver 真是个纠结的问题.找了许久. 终于发现Intent中传递了一个参数(自定义类继 ...
- 使用WIF实现单点登录Part IV —— 常见问题
InvalidOperationException: ID1073: 尝试使用 ProtectedData API 解密 Cookie 时出现 CryptographicException (有关详细 ...
- pure的bug记录2
<select id="stacked-state" style=" font-family: "Microsoft YaHei"; " ...
- 删除字符串第一个byte
删除字符串第一个byte 一种方式: char * mag; char buff[1000]; char number; memcpy((char *)msg,buff,len); strnc ...
- hdu 4609 3-idiots <FFT>
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意: 给定 N 个正整数, 表示 N 条线段的长度, 问任取 3 条, 可以构成三角形的概率为多 ...
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...