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题解的更多相关文章

  1. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  2. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  3. Leetcode Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  4. Leetcode: Binary Tree Postorder Transversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  5. [Leetcode] Binary tree postorder traversal二叉树后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  6. [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  7. LeetCode——Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  8. LeetCode: Binary Tree Postorder Traversal [145]

    [题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...

  9. LeetCode Binary Tree Postorder Traversal(数据结构)

    题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...

随机推荐

  1. springmvc 通过@ResponseBody 返回json的中文乱码解决方案2个

    1.方法上面的RequestMapping要加上红色的部分.   @ResponseBody     @RequestMapping(value = "/search", prod ...

  2. Java_多线程2_线程池

    线程池(pool): 线程池的作用: 1.节省资源,减少线程的数量和创建销毁线程的开销2.合理的管理线程的分配 线程池的创建: 1.newCachedThreadPool //优点:很灵活,弹性的线程 ...

  3. date命令的基本用法

    date设置时间 设置时间:-s参数 date -s "20190426 15:22:33" date查看时间差  (-d参数多用于脚本) 查看时间差:-d参数date -d &q ...

  4. centos 7 查看所有登录用户的操作历史

    2019-01-07 转自  https://www.cnblogs.com/kevingrace/p/7373146.html centos 7 查看所有登录用户的操作历史 在Linux系统的环境下 ...

  5. Java 语言结构【转】

    Java 语言结构 基础:包(Package).类(Class)和对象(Object) 了解 Java 的包(Package).类(Class)和对象(Object)这些基础术语是非常重要的,这部分内 ...

  6. mysql中对my.cnf进行说明

    my.cnf说明: #vim /etc/my.cnf以下只列出my.cnf文件中[mysqld]段落中的内容,其他段落内容对MySQL运行性能影响甚微,因而姑且忽略. [mysqld] port =  ...

  7. shiro学习笔记_0700_整合ssm

    现在最流行的框架就是ssm,学到最后,shiro在实际开发中,也就的整合框架.首先spring是少不了的,shiro也提供了和spring的整合包. 首先,新建maven项目: maven依赖: &l ...

  8. jQuery事件之传递参数

    一.jQuery绑定事件的三种方法 我们这里首先复习一下jQuery绑定事件的三种方法: target.click(function(){}); target.on("click" ...

  9. 解决UnicodeDecodeError: 'ascii' code can't decode byte 0xef in position

    今天在使用python的pip安装的时候出现了这个错误 UnicodeDecodeError: 'ascii' code can't decode byte 0xef in position 7: o ...

  10. Bunder: What does :require => nil in Gemfile mean?

    https://stackoverflow.com/questions/12200215/bunder-what-does-require-nil-in-gemfile-mean Require ni ...