Binary Tree Postorder Traversal

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

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

解法一:递归法

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ret;
Helper(ret, root);
return ret;
}
void Helper(vector<int>& ret, TreeNode* root)
{
if(root != NULL)
{
Helper(ret, root->left);
Helper(ret, root->right);
ret.push_back(root->val);
}
}
};

解法二:借助栈的深度优先搜索,需要记录每个节点是否访问过。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ret;
if(root == NULL)
return ret;
stack<TreeNode*> stk;
unordered_map<TreeNode*, bool> visited;
stk.push(root);
visited[root] = true;
while(!stk.empty())
{
TreeNode* top = stk.top();
if(top->left != NULL && visited[top->left] == false)
{
stk.push(top->left);
visited[top->left] = true;
continue;
}
if(top->right != NULL && visited[top->right] == false)
{
stk.push(top->right);
visited[top->right] = true;
continue;
}
ret.push_back(top->val);
stk.pop();
}
return ret;
}
};

解法三:在Discussion看到一种巧妙的解法。

前序是:根左右

后序是:左右跟

因此可以将前序改为根右左,然后逆序为左右根输出。

前序遍历不需要回溯(对应图的深度遍历),是一种半层次遍历,因此效率很高。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ret;
if(root == NULL)
return ret;
stack<TreeNode*> stk;
stk.push(root);
while(!stk.empty())
{
TreeNode* top = stk.top();
stk.pop();
ret.push_back(top->val);
if(top->left != NULL)
stk.push(top->left);
if(top->right != NULL)
stk.push(top->right);
}
reverse(ret.begin(), ret.end());
return ret;
}
};

【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)的更多相关文章

  1. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  2. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  3. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  4. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  5. LeetCode OJ 145. Binary Tree Postorder Traversal

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

  6. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  7. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 相似题目 参考资料 日期 题目地址:htt ...

  9. 【LeetCode】94. Binary Tree Inorder Traversal

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

随机推荐

  1. 卷积神经网络用于视觉识别Convolutional Neural Networks for Visual Recognition

    Table of Contents: Architecture Overview ConvNet Layers Convolutional Layer Pooling Layer Normalizat ...

  2. Why游戏作品合集

    之前曾经发过一个套WhyEngine游戏作品合集,里面有十几个小游戏和若干个屏保程序和若干个DEMO程序.而这次发的与上次不一样,因为这是我花了两天时间将所有的程序集成到一个工程后的成果.为了能将所有 ...

  3. Python源码学习七 .py文件的解释

    Python源码太复杂了... 今天看了下对.py文件的parse, 云里雾里的 py文件是最简单的, 在python的交互式窗口 import这个模块 a = 10 print(a) 开始分析,堆栈 ...

  4. pymysql的使用心得(1)------小细节,注意!

    最近一段时间开始使用MySQL,使用的是pymysql库. 其中遇到过一些小问题,值得记录一下,以便今后使用的时候注意到. 表格的建立,代码如下: cursor.execute("creat ...

  5. iOS开发-UIApplication和App启动状态

    UIApplication简单从字面上了解就是应用程序,开发的时候有的时候会根据需要调用其中的方法,看起来不起眼,实际在iOS开发UIApplication提供了iOS程序运行期间的控制和协作工作.每 ...

  6. C#(64位系统) 解决"未能加载文件或程序集,或它的某一个依赖项..."

    这个问题通常出在引用第三方DLL或者自己以前写的DLL. 在64位系统下则可能会出现这种问题. 今天下载MySQLDriverCS后引用遍出现了这个问题,参考了一些文档,下面给出解决方法: 将项目的生 ...

  7. C#: 实现支持断点续传多线程下载

    /* .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient)* Reflector 了一下 System.Net.WebClien ...

  8. Design Your Own Protocol In Five Minutes

    https://mayaposch.wordpress.com/2011/10/03/design-your-own-protocol-in-five-minutes ---------------- ...

  9. BSTR

    BSTR A BSTR (Basic string or binary string) is a string data type that is used by COM, Automation, a ...

  10. 为什么要使用href=”javascript:void(0);”?

    JavaScript中语句最后的分号是可以缺省的,那为何要使用javascript:;而不是javascript:呢? 是习惯还是规范,我疑惑了! 具有代码洁癖的coder们,没事多写一个分号,圣洁的 ...