zoukankan      html  css  js  c++  java
  • Leetcode: Binary Tree Postorder Transversal

    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?

    难度:70

    recursive方法很直接:

     1 public ArrayList<Integer> postorderTraversal(TreeNode root) {
     2     ArrayList<Integer> res = new ArrayList<Integer>();
     3     helper(root, res);
     4     return res;
     5 }
     6 private void helper(TreeNode root, ArrayList<Integer> res)
     7 {
     8     if(root == null)
     9         return;
    10     helper(root.left,res);
    11     helper(root.right,res);
    12     res.add(root.val);
    13 }

    Iterative 最优做法:

    pre-order traversal is root-left-right.

    post-order traversal is left-right-root.

    We can modify pre-order traversal to be root-right-left, and traverse the tree. 

    Finally, we reverse the output by the modified pre-order traversal to get post-order traversal.

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public List<Integer> postorderTraversal(TreeNode root) {
    12         LinkedList<Integer> res = new LinkedList<>();
    13         Stack<TreeNode> stack = new Stack<>();
    14         TreeNode p = root;
    15         while (p!=null || !stack.isEmpty()) {
    16             if (p != null) {
    17                 stack.push(p);
    18                 res.addFirst(p.val);
    19                 p = p.right;
    20             }
    21             else {
    22                 TreeNode node = stack.pop();
    23                 p = node.left;
    24             }
    25         }
    26         return res;
    27     }
    28 }

    第一次Iterative的做法就没有那么strait forward的了,需要额外用一个ArrayList<TreeNode>来记录节点的访问情况

     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public List<Integer> postorderTraversal(TreeNode root) {
    12         ArrayList<Integer> res = new ArrayList<Integer>();
    13         if (root == null) return res;
    14         ArrayList<TreeNode> visited = new ArrayList<TreeNode>();
    15         LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
    16         stack.push(root);
    17         while (root != null || !stack.isEmpty()) {
    18             if (root.left != null && !visited.contains(root.left)) {
    19                 stack.push(root.left);
    20                 root = root.left;
    21             }
    22             else if (root.right != null && !visited.contains(root.right)) {
    23                 stack.push(root.right);
    24                 root = root.right;
    25             }
    26             else {
    27                 visited.add(root);
    28                 res.add(stack.pop().val);
    29                 root = stack.peek();
    30             }
    31         }
    32         return res;
    33     }
    34 }
  • 相关阅读:
    Codeforces Round #371 (Div. 1)
    Making the Grade(POJ3666)
    The trip(Uva 11100)
    Codeforces Round #370 (Div. 2) E. Memory and Casinos (数学&&概率&&线段树)
    [CodeForces
    勾股数组 学习笔记
    NOIP 2015 游记
    BestCoder Round #53 (div.1)
    北大信息学夏令营 游记
    Codeforces Round #313 (Div. 1)
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3972302.html
Copyright © 2011-2022 走看看