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;}
};

转载于:https://www.cnblogs.com/liangf27/p/9356871.html

[LeetCode] Binary Tree Postorder题解相关推荐

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

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

  2. [leetcode]Binary Tree Postorder TraversalBinary Tree Preorder Traversal

    题意:给出了一颗二叉树,求二叉树的前序遍历和后序遍历,题中要求尽量使用非递归方法遍历 后序遍历递归代码: /*** Definition for binary tree* struct TreeNod ...

  3. LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法

    题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...

  4. 【二叉树的迭代版后序遍历】LeetCode 145. Binary Tree Postorder Traversal

    LeetCode 145. Binary Tree Postorder Traversal Solution1:递归版答案 二叉树的后序遍历递归版是很简单的,关键是迭代版的代码既难理解又难写!但听了花 ...

  5. [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  6. 5. Binary Tree Postorder Traversal

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

  7. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

  8. LeetCode 145. Binary Tree Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-postorder-traversal/ 题目: Given a binary tree, retu ...

  9. LeetCode:145. Binary Tree Postorder Traversal

    总共想出了四种方法来做. 第一种是递归,不说了.后面集中都是迭代. 第二种是使用一个栈,而且借助一个set来记录哪些node是访问过它的子节点的,防止重复访问.缺点是需要额外的set的空间 class ...

最新文章

  1. UISegmentedControl 分段器加载不同的viewcontroller
  2. YUIDoc的使用方法小结
  3. 桌面计算机打开不了怎么办,电脑桌面上的所有东西都打不开了 怎么处理
  4. 异常org.xmlpull.v1.XmlPullParserException
  5. labelme安装_语义图像分割-DIGITS2-labelme数据集自动扩展
  6. 奇安信代码安全实验室帮助 RedHat 修复两个 oVirt 漏洞,获官方致谢
  7. SSM项目实战:App信息管理平台(含源码与设计分析)
  8. Android flag详解
  9. Leetcode——四数之和问题
  10. 【Day4.7】错过湄南河夜游,去河畔夜市晚餐
  11. PHP获取字符串长度之strlen和mb_strlen的区别
  12. 朋友——friends
  13. php工具箱在win10自动退出,win10游戏闪退修复办法
  14. golang的运维开发
  15. 深度学习利器之自动微分(2)
  16. [算法] 高斯消元详解
  17. 80年代个人计算机,现在的电脑程序在80年代就已经有了
  18. Android LCD(三):LCD接口篇【转】
  19. Hive的学习和使用
  20. 直击安全狗攻防武道大赛:完美防护与技术牛人的巅峰对决

热门文章

  1. Python 学习 —— Numpy 、Pandas 傻傻分不清楚
  2. 上拉电阻和下拉电阻的作用详解
  3. 网络教育计算机二级题库2021,2021计算机二级office
  4. c语言中一百以内相乘的积,一百以内的加减乘除法游戏....
  5. synchronize与lock的区别
  6. equals方法变量和常量位置区别
  7. 【Spring注解】@Condition条件注册
  8. 获取mysql可行方法_Mysql学习Java实现获得MySQL数据库中所有表的记录总数可行方法...
  9. java 微网站_java架构之路-(微服务专题)初步认识微服务与nacos初步搭建
  10. Android安全加密:Https编程