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. IIS发布好的网页突然不显示图片了

    按以下步骤把地址加到ie的本地intranet就好了

  2. BZOJ4283: 魔法少女伊莉雅(最短路径图+最短路径树)

    题面 传送门 题解 太长了不想写了→_→ 题解 //minamoto #include<bits/stdc++.h> #define R register #define inf 0x3f ...

  3. 机器学习笔记(五) K-近邻算法

    K-近邻算法 (一)定义:如果一个样本在特征空间中的k个最相似的样本中的大多数属于某一个类别,则该样本也属于这个类别. (二)相似的样本,特征之间的值应该是相近的,使用k-近邻算法需要做标准化处理.否 ...

  4. ubuntu 14.04网卡配置以及关闭防火墙

    一.Ubuntu网卡配置如下: 在文件/etc/network/interfaces中进行以下配置 auto lo iface lo inet lookback auto eth0 iface eth ...

  5. 实现基于NTP协议的网络校时功能

    无论PC端还是移动端系统都自带时间同步功能,基于的都是NTP协议,这里使用C#来实现基于NTP协议的网络校时功能(也就是实现时间同步). 1.NTP原理 NTP[Network Time Protoc ...

  6. jmeter笔记

    Jmeter性能测试 入门 Jmeter 录制脚本:使用一个叫badbody的工具录制脚步供jmeter使用,http://www.badboy.com.au/:也可以用jmeter来录制 Jmete ...

  7. 学习并发包常用的接口----java.util.concurrent

    1.常用的相关的接口 Callable.(Runnable).Futrue.RunnableFuture.RunnableSheduledFuture.ScheduledFuture.Executor ...

  8. json test

    public static void main(String[] args) { class B { String name; public String getName() { return nam ...

  9. 2.2.2 加入factory机制

    上一节给出了一个只有driver.使用UVM搭建的验证平台.严格来说这根本就不算是UVM验证平台,因为UVM的特性几乎一点都没有用到.像上节中my_driver的实例化及drv.main_phase的 ...

  10. malloc/free 与 new/delete 区别

    malloc/free 是c++/c的标准库函数,new/delete 是c++的运算符.两者都可以申请动态内存和释放内存.对于非内部数据类型的对象而言,光用malloc/free是无法满足动态对象的 ...