题目:

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     public void helper(TreeNode root, ArrayList<Integer> re){
 2         if(root==null)
 3             return;
 4         
 5         helper(root.left,re);
 6         helper(root.right,re);
 7         re.add(root.val);
 8     }
 9     public ArrayList<Integer> postorderTraversal(TreeNode root) {
         ArrayList<Integer> re = new ArrayList<Integer>();
         if(root==null)
             return re;
         helper(root,re);
         return re;
     }

非递归方法代码:

引用自Code ganker:http://blog.csdn.net/linhuanmars/article/details/22009351

“接下来是迭代的做法,本质就是用一个栈来模拟递归的过程,但是相比于Binary
Tree Inorder Traversal
Binary
Tree Preorder Traversal
,后序遍历的情况就复杂多了。我们需要维护当前遍历的cur指针和前一个遍历的pre指针来追溯当前的情况(注意这里是遍历的指针,并不是真正按后序访问顺序的结点)。具体分为几种情况:
(1)如果pre的左孩子或者右孩子是cur,那么说明遍历在往下走,按访问顺序继续,即如果有左孩子,则是左孩子进栈,否则如果有右孩子,则是右孩子进栈,如果左右孩子都没有,则说明该结点是叶子,可以直接访问并把结点出栈了。
(2)如果反过来,cur的左孩子是pre,则说明已经在回溯往上走了,但是我们知道后序遍历要左右孩子走完才可以访问自己,所以这里如果有右孩子还需要把右孩子进栈,否则说明已经到自己了,可以访问并且出栈了。
(3)如果cur的右孩子是pre,那么说明左右孩子都访问结束了,可以轮到自己了,访问并且出栈即可。
算法时间复杂度也是O(n),空间复杂度是栈的大小O(logn)。实现的代码如下(代码引用自:http://www.programcreek.com/2012/12/leetcode-solution-of-iterative-binary-tree-postorder-traversal-in-java/):”

 1     public ArrayList<Integer> postorderTraversal(TreeNode root) {
 2  
 3         ArrayList<Integer> lst = new ArrayList<Integer>();
 4  
 5         if(root == null)
 6             return lst; 
 7  
 8         Stack<TreeNode> stack = new Stack<TreeNode>();
 9         stack.push(root);
  
         TreeNode prev = null;
         while(!stack.empty()){
             TreeNode curr = stack.peek();
  
             // go down the tree.
             //check if current node is leaf, if so, process it and pop stack,
             //otherwise, keep going down
             if(prev == null || prev.left == curr || prev.right == curr){
                 //prev == null is the situation for the root node
                 if(curr.left != null){
                     stack.push(curr.left);
                 }else if(curr.right != null){
                     stack.push(curr.right);
                 }else{
                     stack.pop();
                     lst.add(curr.val);
                 }
  
             //go up the tree from left node    
             //need to check if there is a right child
             //if yes, push it to stack
             //otherwise, process parent and pop stack
             }else if(curr.left == prev){
                 if(curr.right != null){
                     stack.push(curr.right);
                 }else{
                     stack.pop();
                     lst.add(curr.val);
                 }
  
             //go up the tree from right node 
             //after coming back from right node, process parent node and pop stack. 
             }else if(curr.right == prev){
                 stack.pop();
                 lst.add(curr.val);
             }
  
             prev = curr;
         }
  
         return lst;
     }

Binary Tree Postorder Traversal leetcode java的更多相关文章

  1. Binary Tree Preorder Traversal leetcode java

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

  2. Binary Tree Inorder Traversal leetcode java

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

  3. Binary Tree Postorder Traversal --leetcode

    原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目大意:后序遍历二叉树 解题思路:后序遍历二叉树的步骤: ...

  4. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

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

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

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

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

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

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

  8. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  9. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

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

随机推荐

  1. chrome安装(sentos7)

    在服务器上安装chrome是用来模拟浏览器抓取数据的. 直接 yum install chrome是安装不了的 你要做以下几步就可以了. 配置yum源 1. vim /etc/yum.repos.d/ ...

  2. 学习 HMM

    简介 HMM 中的变量可以分为两组. 第一组是状态变量 \(\{y_i,y_2,\cdots, y_n\}\), 其中 \(y_i \in \mathcal{Y}\) 表示第 \(i\) 时刻的系统状 ...

  3. 【mysql】当where后接字符串,查询时会发生什么?

    好久没有研究一个“深层次”的问题了. 首先来看我们为什么要讨论这个问题~ 首先这是一个正常的数据库查询,我们可以看到在ruizhi数据库里的chouka表内,所有数据如图. 现在,我们运行查询: se ...

  4. Java反射机制demo(三)—获取类中的构造函数

    Java反射机制demo(三)—获取类中的构造函数 1,获取类中所有的构造函数 如下面的代码中所示,这个类中显式的构造函数有五个. 空构造: public UserInfo() 带参构造有四个: pu ...

  5. redis 主要数据类型及使用

    1.类型 redis 的主要数据类型: 1.1 string 字符串类型<*是其它4种类型的基础> 1.2 hash 散列类型 1.3 list 列表类型 1.4 set 集合类型 1.5 ...

  6. 【SQL】181. Employees Earning More Than Their Managers

    The Employee table holds all employees including their managers. Every employee has an Id, and there ...

  7. uboot的使用

    嵌入式软件的层次: bootloader +boot_parameter+kernel+ boot filesystem <uboot的编译> 1)将uboot压缩文件拷贝到 linux系 ...

  8. 【BZOJ 3036】 3036: 绿豆蛙的归宿 (概率DP)

    3036: 绿豆蛙的归宿 Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 491  Solved: 354 Description 随着新版百度空间的下线 ...

  9. ubuntu18.04 安装Navicat 解决字体方框问题

    前景 最近带着看一点数据库的知识,装一下navicat,就是这个玩意儿,在我编码毫无问题的情况下,这个软件上却显示各种乱码 环境 ubuntu 18.04 navicat 12(最新版) mysql ...

  10. PHP 笔记——面向对象编程知识点

    类是属性和方法的集合,是面向对象编程方式的核心和基础,通过类可以将零散的用于实现某项功能的代码进行有效管理. 类是由class关键字.类名和成员组成的,类名不区分大小写. 在类中定义常量使用关键字 c ...