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?

1.递归算法的递归定义是:

若二叉树为空,则遍历结束;否则
⑴ 后序遍历左子树(递归调用本算法);
⑵ 后序遍历右子树(递归调用本算法) ;
⑶ 访问根结点 。

对有n个结点的二叉树,其时间复杂度均为O(n) 。

 List<Integer> postOrder = new ArrayList<Integer>();
public List<Integer> postorderRecursion(TreeNode root) {
if ((root != null) && (root.val != '#')) {
postorderRecursion(root.left);
postorderRecursion(root.right);
postOrder.add(root.val);
}
return postOrder;
}

2.非递归算法

在后序遍历中,根结点是最后被访问的。因此,在遍历过程中,当搜索指针指向某一根结点时,不能立即访问,而要先遍历其左子树,此时根结点进栈。当其左子树遍历完后再搜索到该根结点时,还是不能访问,还需遍历其右子树。所以,此根结点还需再次进栈,当其右子树遍历完后再退栈到到该根结点时,才能被访问。

因此,设立一个状态标志变量tag :{ 0 : 结点暂不能访问;1 : 结点可以被访问}。

其次,设两个堆栈S1、S2 ,S1保存结点,S2保存结点的状态标志变量tag 。S1和S2共用一个栈顶指针。

设T是指向根结点的指针变量,非递归算法是:

若二叉树为空,则返回;否则,令p=T;

⑴ 第一次经过根结点p,不访问:

p进栈S1 , tag 赋值0,进栈S2,p=p->Lchild 。

⑵ 若p不为空,转(1),否则,取状态标志值tag :

⑶ 若tag=0:对栈S1,不访问,不出栈;修改S2栈顶元素值(tag赋值1) ,取S1栈顶元素的右子树,即p=S1[top]->Rchild ,转(1);

⑷ 若tag=1:S1退栈,访问该结点;

直到栈空为止。

 List<Integer> postOrder = new ArrayList<Integer>();
public List<Integer> postorderTraversal(TreeNode p) {
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<Boolean> tag = new Stack<Boolean>();
while ((p != null) || !stack.isEmpty()) {
if (p != null) {
stack.push(p);
tag.push(false);
p = p.left;
} else {
boolean visit = tag.pop();
if (visit) {
postOrder.add(stack.pop().val);
} else {
tag.push(true);
p = stack.peek().right;
}
}
}
return postOrder;
}

二叉树的三种遍历递归和非递归实现:递归实现都简单;非递归的前序和中序实现简单,后序采用2个栈来实现(参考严蔚敏的思路,比较容易理解)。代码如下:

 import java.util.ArrayList;
import java.util.List;
import java.util.Stack; import javax.swing.text.AbstractDocument.LeafElement; /**
* Definition for binary tree public class TreeNode { int val; TreeNode left;
* TreeNode right; TreeNode(int x) { val = x; } }
*/
public class TreeNodeSolution { public List<Integer> preorderRecursion(TreeNode root) {
List<Integer> preOrder = new ArrayList<Integer>();
if ((root != null) && (root.val != '#')) {
preOrder.add(root.val);
postorderRecursion(root.left);
postorderRecursion(root.right);
}
return preOrder;
} public List<Integer> inorderRecursion(TreeNode root) {
List<Integer> inOrder = new ArrayList<Integer>();
if ((root != null) && (root.val != '#')) {
postorderRecursion(root.left);
inOrder.add(root.val);
postorderRecursion(root.right);
}
return inOrder;
} public List<Integer> postorderRecursion(TreeNode root) {
List<Integer> postOrder = new ArrayList<Integer>();
if ((root != null) && (root.val != '#')) {
postorderRecursion(root.left);
postorderRecursion(root.right);
postOrder.add(root.val);
}
return postOrder;
} public List<Integer> inorderTraversal(TreeNode p) {
List<Integer> inOrder = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while ((p != null) || !stack.isEmpty()) {
if (p != null) {
stack.push(p);
p = p.left;
} else {
p = stack.pop();
inOrder.add(p.val);
p = p.right;
}
}
return inOrder;
} public List<Integer> preorderTraversal(TreeNode p) {
List<Integer> preOrder = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while ((p != null) || !stack.isEmpty()) {
if (p != null) {
preOrder.add(p.val);
stack.push(p);
p = p.left;
} else {
p = stack.pop();
p = p.right;
}
}
return preOrder;
} public List<Integer> postorderTraversal(TreeNode p) {
List<Integer> postOrder = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<Boolean> tag = new Stack<Boolean>();
while ((p != null) || !stack.isEmpty()) {
if (p != null) {
stack.push(p);
tag.push(false);
p = p.left;
} else {
boolean visit = tag.pop();
if (visit) {
postOrder.add(stack.pop().val);
} else {
tag.push(true);
p = stack.peek().right;
}
}
}
return postOrder;
} public static void main(String[] args) {
TreeNode t1 = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
t1.setRight(t2);
t1.setLeft(t3);
System.out.println(new TreeNodeSolution().postorderTraversal(t1));
}
}

LeetCode解题报告:Binary Tree Postorder Traversal的更多相关文章

  1. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

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

  2. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

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

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

  4. LeetCode OJ 145. Binary Tree Postorder Traversal

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

  5. LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)

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

  6. 【LeetCode】145. Binary Tree Postorder Traversal

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

  7. LeetCode题解之Binary Tree Postorder Traversal

    1.题目描述 2.问题分析 递归 3.代码 vector<int> postorderTraversal(TreeNode* root) { vector<int> v; po ...

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

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

  9. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

随机推荐

  1. WTL的核心机制

    WTL背景介绍 WTL是微软ATL开发组成员Nenad Stefanovic先生在ATL Windowing机制上发展起来的一整套GUI框架,运用template技术组织和创建GUI对象,构筑了精致的 ...

  2. springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)

    springMvc中406错误解决, springMvc使用json出现406 (Not Acceptable) >>>>>>>>>>> ...

  3. Linq扩展方法之All 、Any

    // Summary: // 确定序列中的所有元素是否满足条件. // Parameters: // source:包含要应用谓词的元素的 System.Collections.Generic.IEn ...

  4. css中判断IE版本的语句

    css中判断IE版本的语句<!--[if gte IE 6]> Only IE 6/+ <![endif]-->: 1. <!--[if !IE]> 除IE外都可识 ...

  5. AngularJS code converage

    karma-coverage The easiest way is to keep karma-coverage as a devDependency in your package.json. Mo ...

  6. 本地tomcat的start.bat启动时访问不出现小猫图标

    排除端口错误.看看是不是webapps的root文件夹删除了,如果删除了,从tomcat的压缩包中解压一个root文件夹,房里面即可

  7. MyBatis的学习总结六:Mybatis的缓存【参考】

    一.Mybatis缓存介绍 正如大多数持久层框架一样,Mybatis同样提供了一级缓存和二级缓存 1.一级缓存:基于PerpetualCache的HashMap本地缓存,其存储作用域为Session, ...

  8. 3.SQL*Plus命令

    3.1SQL*Plus与数据库的交互 主要用来数据库查询和数据处理的工具. 3.2SQL*Plus运行环境设置 3.2.1SET命令概述 用户可以使用SET命令设置SQL*Plus的运行环境,SET命 ...

  9. BearSkill实用方法之UITextField限制输入的字符数量

    原文:http://blog.csdn.net/xiongbaoxr/article/details/51525061      

  10. Constructor and destructor -- Initialization & Cleanup in C++

    Why need initialization and cleanup? A large segment of C bugs occur when the programmer forgets to ...