Binary Tree Postorder Traversal leetcode java

题目:

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) {
10         ArrayList<Integer> re = new ArrayList<Integer>();
11         if(root==null)
12             return re;
13         helper(root,re);
14         return re;
15     }

非递归方法代码:

引用自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)。实现的代码如下:”

1 public ArrayList<Integer> postorderTraversal(TreeNode root) {
 2     ArrayList<Integer> res = new ArrayList<Integer>();
 3     if(root == null)
 4         return res;
 5     LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
 6     stack.push(root);
 7     TreeNode pre = null;
 8     while(!stack.isEmpty()){
 9         TreeNode cur = stack.peek();
10         if(pre==null || pre.left==cur || pre.right==cur){
11             if(cur.left!=null)
12                 stack.push(cur.left);
13             else if(cur.right!=null)
14                 stack.push(cur.right);
15             else
16                 res.add(cur.val);
17                 stack.pop();
18         }
19         else if(cur.left==pre && cur.right!=null){
20             stack.push(cur.right);
21         }else{
22             res.add(cur.val);
23             stack.pop();
24         }
25         pre = cur;
26     }
27     return res;
28 }

Binary Tree Postorder Traversal leetcode java

时间: 2024-08-26 02:47:50

Binary Tree Postorder Traversal leetcode java的相关文章

Binary Tree Preorder Traversal leetcode java

题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题解: 递归做法如下: 1     public void helper(Tree

Binary Tree Inorder Traversal leetcode java

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 题解: 中序遍历:递归左 处理当前 递归右. 画图的话就是,之前离散老师教的,从ro

Binary Tree Postorder Traversal &lt;leetcode&gt;

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.非递归遍历  2.递归遍历 非递归: 1 /** 2 * Definition

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

Binary Tree PreOrder Traversal: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3   return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 不使用递归前序遍历,可以

LeetCode解题报告: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? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

LeetCode: Binary Tree Postorder Traversal 解题报告

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    /   3return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively

[Leetcode][Tree][Binary Tree Postorder Traversal]

二叉树的后续遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsPostorderTraversal(TreeNode *now, vec

【Leetcode】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? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

leetcode题解: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? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时