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;
        }
    }
  • 相关阅读:
    前台 图片上传 上传预览 调用上传服务(多张图片展示)
    正则表达式验证,只能输入数字
    点击文本框搜索,出现在下拉列表中
    keycode 锁键盘按键(只能输入数字)
    Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    map 理解
    mybatis 关联关系查询 java
    mybatis 批量插入值的sql
    EJB
    JPA概要
  • 原文地址:https://www.cnblogs.com/airwindow/p/4216213.html
Copyright © 2011-2022 走看看