[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 ...
随机推荐
- 【转载】hadoop之failed task任务和killed task任务
failed task可理解为自杀,也就是task本身出了问题而自杀:killed task可理解为是他杀,也就是jobtracker认为这个任务的执行是多余的,所以把任务直接杀掉.起初用hadoop ...
- C语言 一些算法
1,斐波那契数列 ①递归 时间复杂度O(2^n)#include <stdio.h> int fib(int n){ ||n==) ; ) + fib(n-); } int main(){ ...
- JMeter组件之BeanShell PostProcessor的使用
1. 场景一:获取请求响应中的数据,并保存 import com.alibaba.fastjson.*; // 引入包.这个包需要先放在:<安装目录>\apache-jmeter-3.2 ...
- 修改linux的文件时,如何快速找到要修改的内容
♦ 在linux系统下,找到需要修改的文件.使用cd+目录的命令进行文件所在的目录,使用ls命令查看是否有该文件. ♦ 使用vim+文件名,打开该文件 ♦ 快速在文件中找到需要修改的地方.如我们需要修 ...
- Ionic2 启动加载优化总结
1. ionic2通过ionic serve生成的main.js大于4M,必须先build才能部署 npm run ionic:build --prod 之后main.js缩小为大概100K+ 2. ...
- iOS --UIScrollView的学习(二)
1.接着上一次的说:http://www.cnblogs.com/fengzhihao/p/5287734.html,这次讲一下UISCrollView的缩放功能. 2.当用户在UIScrollVie ...
- docker微服务部署之:一,搭建Eureka微服务项目
先说明一下docker需要搭建的微服务的基本情况: 项目情况:一个demo_parent项目,下面三个子模块:demo_eureka(eureka服务).demo_article(文章服务).demo ...
- CUDA安装
1.CUDA是什么? CUDA(Compute Unified Device Architecture),显卡厂商NVidia推出的运算平台. 随着显卡的发展,GPU越来越强大,而且GPU为显示图像做 ...
- [原创] Trie树 php 实现敏感词过滤
目录 背景 简介 存储结构 PHP 其他语言 字符串分割 示例代码 php 优化 缓存字典树 常驻服务 参考文章 背景 项目中需要过滤用户发送的聊天文本, 由于敏感词有将近2W条, 如果用 str_r ...
- The Annoying Bug
log里看不出问题,直接客户端就disconnected. gdb 挂了也不会停住,继续跑得跟正常人似的 再连根本不正常的了. 硬件: a , 主板CPU更换过 b,USB3.0 软件: 无有更换,但 ...