zoukankan      html  css  js  c++  java
  • [LeetCode#145]Binary Tree Postorder Traversal

    The problem:

    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].

    The recursive solution:

    public class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            
            ArrayList<Integer> ret = new ArrayList<Integer> ();
            
            if (root == null)
                return ret;
            
            helper(root, ret);
            return ret;
        }
        
        private void helper(TreeNode cur_root, ArrayList<Integer> ret) {
            
            if (cur_root == null)
                return;
            
            helper(cur_root.left, ret);
            helper(cur_root.right, ret);
            ret.add(cur_root.val);
        }
    }

    The iterative analysis:

    Unlike the preorder and inorder traversal, we could add the current node's value, either when it is firstly encountered(preorder) or when it is poped out from the stack(inorder). 
    The problem invloves more complex problems, cause we need to add a node(value) into the answers set after fininshing the traversal over left-sub tree and right-sub tree. The properity of post order determines each node has to be poped from the stack twice.
    1. the first pop is to get the pointer to scan right child.
    2. the second pop is to add the node into the answer set, aftering finishing the traversal over the right sub-tree.
    
    How to distiguish those two cases from the poped node?
    skill: use a HashMap is a great way to solve this problem!
    HashMap<TreeNode, Integer> check_map = new HashMap<TreeNode, Integer>();
    
    1. when we meet the node first time, we push the node into stack and put the node into the hashmap with value 0.
    check_map.put(root, 0);
    
    2. when we pop a node from the stack, we check the value of this node's value in hashmap.
        2.1 iff the node's value is 0, it means this node was poped from the stack first time. 
            2.1.1 we need to push the node back into the stack again.
            2.1.2 we also need to put the node back into the haspmap with the updated value: 1. 
            2.1.3 we need to update the root pointer to point the root's right child. 
        
        2.2 iff the node's value is 1, it means this node was poped from the stack second time, which means we have already finish          the traversal over it left and right sub-trees.
            2.2.1 we need to add the node into the answer set.
            2.2.2 we need to set the root's pointer to null, since we have already fininshed the traversal over this tree.
    
    Note:
    In both situation 2.1 and 2.2, we update the root pointer, otherwise it would result in a infinite loop over a node. This is the reason why can use  "if (root != null)" for next iteration. 

    The iterative solution:

    public class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            
            ArrayList<Integer> ret = new ArrayList<Integer> ();
            Stack<TreeNode> stack = new Stack<TreeNode> ();
            HashMap<TreeNode, Integer> check_map = new HashMap<TreeNode, Integer>();
            
            while (root != null || stack.empty() != true) {
                
                if (root != null) {
                    
                    stack.push(root);
                    check_map.put(root, 0);
                    root = root.left;
                } else {
                    
                    root = stack.pop();
                    if (check_map.get(root) == 0) {// the first time pop
                        
                        stack.push(root); //push the root node back(post order traversal)
                        check_map.put(root, 1);//note the order with updating root value
                        root = root.right;
                    } else {//the second time pop
                        
                        ret.add(root.val);
                        root = null; //it's very important!!!
                    }
                }
            }
            return ret;
        }
    }
  • 相关阅读:
    AFN的配置使用
    如何给另外一台电脑(同组其他成员)开发ios权限
    IAP中的坑
    如何在上架app后修改app在商店中的名字
    wordpress禁止调用官方Gravatar头像调用ssl头像链接提升加载速度
    ubuntu 杀死进程命令
    tp框架 php5.5以上版本出现”No input file specified“错误问题解决
    支付宝调用错误:Call to undefined function openssl_sign()
    浏览器下出现net::ERR_BLOCKED_BY_CLIENT的解决办法
    python 整形方法
  • 原文地址:https://www.cnblogs.com/airwindow/p/4216213.html
Copyright © 2011-2022 走看看