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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- jQuery类级别插件--返回顶部,底部,去到任何部位
先引入js:<script type="text/javascript" src="jquery.js" ></script><s ...
- 为hbase新增节点
为hbase增加新的节点,首先要为hadoop增加新新街点.因为我的做法是将datanode和regionserver放到一台物理机上.因此大体流程是: 1.克隆已经存在的regionserver虚拟 ...
- ThinkPHP常用变量
__ROOT__ : 网站根目录地址 __APP__ : 当前项目(入口文件)地址 __GROUP__:当前分组地址 __URL__ : 当前模块地址 __ACTION__ : 当前操作地址 _ ...
- PHP实习生面经--两天四面
这两天一共面了四家公司,之前投了很多还在想怎么没有叫面试的,后来接连来了四个.下面一个一个做个总结. 1.创想空间(www.quanshi.com) 在五环边上的软件园里,占了一个楼的大概一层吧,算是 ...
- SQL Server 基础:Case两种用法
测试数据 1).等值判断->相当于switch case select S#,C#,C#=( case C# when 1 then '语文' when 2 then '数学' when 3 t ...
- windows下文件名非法字符
/ \ : * ? " < > | / \如果用作文件名,会产生路径问题.因为绝对路径用 \ ; 相对路径用 / ;
- 1.python的第一步
学习python也有一段时间了,自认为基本算是入门了,想要写一些博客进行知识的汇总的时候.却发现不知道该从何说起了,因为python这门语言在语法上其实并不难,关键在于如何建立程序员的思维方式,而对于 ...
- require.js的用法
我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文 ...
- Python实现nb(朴素贝叶斯)
Python实现nb(朴素贝叶斯) 运行环境 Pyhton3 numpy科学计算模块 计算过程 st=>start: 开始 op1=>operation: 读入数据 op2=>ope ...
- 软件工程随堂小作业——寻找“水桶”(C++)
一.设计思想 思路与寻找一个水王相似,这次只是计数器和嫌疑人变量都设置为数组.每次选取一个ID与三个嫌疑人比较,若有相同则计数:若三个都不相同,则三个计数器都减一.若减为0,则从新赋值给嫌疑人. 二. ...