[LeetCode] Binary Tree Postorder题解
Given a binary tree, return the postorder traversal of its nodes’ values.
For example:
Given binary tree {1,#,2,3},return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively?
这是一道LeetCode中标记为Hard的题。事实上如果没有限定不使用递归的话,这道题是非常简单的。所以我只简单回顾一下这道题的两种解法:递归和迭代。
递归法实现后序遍历
算法复杂度为O(n)。
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> re;
print(root,re);
return re;
}
void print(TreeNode *node,vector<int> &re){
if(node == NULL) return;
print(node->left,re);//左
print(node->right,re);//右
re.push_back(node->val);//中
}
};
递归实现前序遍历和后序遍历,只要把print函数中“左右中”三行代码改成相应的顺序即可。
迭代实现后序遍历
迭代实现遍历的本质是广度优先搜索,思路如下:
- Create an empty stack, Push root node to the stack.
- Do following while stack is not empty.
- pop an item from the stack and print it.
- push the left child of popped item to stack.
- push the right child of popped item to stack.
- reverse the ouput.
其中,容易搞错的是输出“中”后,要先push左节点,再push右节点。因为对栈来说,先进去的左节点会后输出(先进后出,后进先出),就实现了“中右左”的顺序,再反转(reverse)就得到了后续遍历(左右中)。
算法复杂度为O(n)。
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> re;
stack<TreeNode*> visit;
if(root != NULL)
visit.push(root);
while(!visit.empty()){
TreeNode *topNode = visit.top();
visit.pop();//top方法只是获取最上面的元素,所以要用pop方法弹出
re.push_back(topNode->val);
if(topNode->left != NULL)
visit.push(topNode->left);
if(topNode->right != NULL)
visit.push(topNode->right);
}
reverse(re.begin(),re.end());
return re;
}
};
[LeetCode] Binary Tree Postorder题解的更多相关文章
- LeetCode: 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 ...
- Leetcode 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 Transversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [Leetcode] 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 dfs,深度搜索
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 binary ...
- LeetCode: Binary Tree Postorder Traversal [145]
[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...
- LeetCode Binary Tree Postorder Traversal(数据结构)
题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...
随机推荐
- Qt5学习笔记(控件)
上面的程序仅仅可以显示一个 大概 的界面,对其进行单击等操作,界面仅有一些简单的反应,对应的程序不能得知界面有什么改变(是否进行单击选择,文本框中是否有文字输入) 下面对程序进行完善. T05Cont ...
- Instant low voltage or power off to make computer power burn down
严重则可造成硬盘直接报废! 原理:瞬间低压或者断电,滤波电容上存储的电能已经被使用,此时再瞬间供电则会重新对电容充电,而限流电阻还没有恢复到保护状态,于是会产生很大的冲击电流,从而导致了全桥元件或保险 ...
- jdk1.6 支持 tls1.2协议 并忽略身份验证
jdk1.6不支持tls1.2协议,jdk1.8默认支持,比较好的解决方案是升级jdk,但是升级jdk风险极大.不能升级jdk的情况下,可以使用如下方式. 引入依赖 <dependency> ...
- springboot多数据源动态切换和自定义mybatis分页插件
1.配置多数据源 增加druid依赖 完整pom文件 数据源配置文件 route.datasource.driver-class-name= com.mysql.jdbc.Driver route.d ...
- max,min无法使用的问题
遇到了包含algorithm头文件以后 min或max函数不能用的问题 改成__min和__max以后就可以了
- 论文笔记:CNN经典结构2(WideResNet,FractalNet,DenseNet,ResNeXt,DPN,SENet)
前言 在论文笔记:CNN经典结构1中主要讲了2012-2015年的一些经典CNN结构.本文主要讲解2016-2017年的一些经典CNN结构. CIFAR和SVHN上,DenseNet-BC优于ResN ...
- 【Quartz】解密properties配置文件中的账号密码
在配置quartz时,为了保密某些信息(特别是账号密码),通常会使用密文.那么在实际使用这些配置信息时,需要进行解密.本文提供一种解密方法如下: (1)假设在properties文件中加密了账号密码 ...
- TX2 刷机时遇到Parsing board information failed
因为之前调试I2C时,修改了EEPROM Layout,所以,在刷机时遇到此问题. 解决办法是按照此文档中的介绍来修改布局. 实际操作时,我拿了一块正常的TX2,按照指令: sudo i2cdump ...
- javascript的简单查询和插入和修改
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Linux下的Shell特殊符号大全(转)
在shell中常用的特殊符号罗列如下: # ; ;; . , / \ 'string'| ! $ ${} $? $$ $* "string"* ** ? : ^ $# $@ `co ...