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?

vector<int> postorderTraversal(TreeNode *root){
vector<int> res;
if(root == NULL) return res;
stack<TreeNode *> record;
record.push(root);
TreeNode *pre = NULL;
while(!record.empty()){
TreeNode *node = record.top();
if((node->left == NULL && node->right == NULL)||(pre!=NULL &&(pre == node->left || pre == node->right)) ){
res.push_back(node->val);
record.pop();
pre = node;
}else{
if(node->right) record.push(node->right);
if(node->left ) record.push(node->left);
}
}
return res;
}

另一种方法:

通过不断的交换左右孩子,然后压栈,最后弹出,即可得到结果

时间复杂度为O(h),h为树的高度,空间复杂度为O(n)

vector<int> postorderTraversa(TreeNode *root){
vector<int> res;
if(root == NULL) return res;
stack<TreeNode *> post_record,reverse_record;
post_record.push(root);
while(!post_record.empty()){
TreeNode *node = post_record.top();
reverse_record.push(node);
post_record.pop();
if(node->left) post_record.push(node->left);
if(node->right) post_record.push(node->right);
}
while(!reverse_record.empty()){
res.push_back(reverse_record.top()->val);
reverse_record.pop();
}
return res;
}

Leetcode Binary Tree Postorder Traversal的更多相关文章

  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 Traversal dfs,深度搜索

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

  5. LeetCode——Binary Tree Postorder Traversal

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

  6. LeetCode: Binary Tree Postorder Traversal [145]

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

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

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

  8. leetcode Binary Tree Postorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  9. leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...

随机推荐

  1. sqlserver执行sql文件命令(sqlcmd)

    个人自用sqlcmd命令: sqlcmd -E -i test.sql -d databasename -s 127.0.0.1 sqlcmd命令解释: 用法: Sqlcmd            [ ...

  2. 与你相遇好幸运,Sail.js创建.sailsrc文件

    在项目根目录下创建.sailsrc文件 {  "generators": {    "modules": {}  },  "hooks": ...

  3. JavaWeb学习之什么JSP、JSP是如何工作的、JSP语言(各种指令和九大内置对象)、EL表达式简单使用(5)

    1.什么JSP * servlet:java编写的处理动态web的技术 * 特点:Java代码中嵌套html代码 * jsp * 特点:HTMl代码中嵌套java代码 * %tomcat%/conf/ ...

  4. Delphi中的各种字符串、String、PChar、Char数组

    参考博客:http://www.cnblogs.com/pchmonster/archive/2011/12/14/2287686.html 其中的所有代码均在Delphi7下测试通过. Delphi ...

  5. 【JAVA多线程问题之死锁】

    一.死锁是什么? 举个例子:两个人一起吃饭,每个人都拿了一只筷子,双方都在等待对方将筷子让给自己,结果两个人都吃不了饭.这种情况和计算机中的死锁情况很相似. 假设有两个线程,互相等待对方释放占有的锁, ...

  6. sqlplus使用(二)

    详见SQL*Plus® User's Guide and Reference Release 11.2   5 Using Scripts in SQL*Plus   1.定义环境变量 _EDITOR ...

  7. <转>Oracle SQL性能优化

    原文链接:http://www.cnblogs.com/rootq/archive/2008/11/17/1334727.html (1)      选择最有效率的表名顺序(只在基于规则的优化器中有效 ...

  8. QQ互联OAuth

    /** * QQ互联 oauth * @author dyllen * */ class Oauth { //取Authorization Code Url const PC_CODE_URL = ' ...

  9. WPF中查看PDF文件

    需要打开PDF文件时,我们第一印象就是使用Adobe Reader.在开发中,经常会遇到需要展示PDF文件的需求.我们会借助于Adobe Reader的Active控件来实现.不过这需要客户的机器上安 ...

  10. (一)WebRTC手记之初探

    转自:http://www.cnblogs.com/fangkm/p/4364553.html WebRTC是HTML5支持的重要特性之一,有了它,不再需要借助音视频相关的客户端,直接通过浏览器的We ...