zoukankan      html  css  js  c++  java
  • [LeetCode#106]Construct Binary Tree from Inorder and Postorder Traversal

    The problem:

    Given inorder and postorder traversal of a tree, construct the binary tree.

    Note:
    You may assume that duplicates do not exist in the tree.

    My analysis:

    This kind of question is elegant and meaningful, always remember following thinking rountine:
    1. How can I get the current array's root?
    1.1 iff preorder series is given, we get it from the first node in the series.
    1.2 iff postorder series is given, we get it form the last node in the series.

    2. How can I divide the arrays into two parts according to the root?
    2.1 iff inorder series is given, we can get the index of the root in it (usually use HashMap).
    Then the left sub-tree's elements(in inorder series) is [inorder_low, index - 1]
    Then the right sub-tree's elements(in inorder series) is [index, inorder_high]

    3. How to get the right partion in preorder/postorder series. (which we must use in each level)
    Key: whether in preorder or postorder, all nodes belonged to left-sub tree must appear before all nodes belonged to right-sub tree. Even the orders in them may be different, we can use the length to make proper boundary.
    In postorder's case. (note: we chop off the last element)
    Inoder series:

    3.1 left sub-tree: inorder[inoder_low, index - 1], 
    3.2 right sub-tree: inorder[index + 1, inorder_high]

    Postorder series: 

    left_high - postorder_low = index - 1 - inorder_low ===> left_high = postorder_low + index - inorder_low - 1;
       
    3.3 left sub-tree: postorder[postorder_low, postorder_low + index - inorder_low - 1], 
    3.4 right sub-tree: postorder[postorder_low + index - inorder_low, postorder_high - 1] 

    My solution:

    public class Solution {
        public TreeNode buildTree(int[] inorder, int[] postorder) {
            
            if (inorder == null || inorder.length == 0)
                return null;
                
            if (postorder == null || postorder.length == 0)
                return null;
            
            int len = inorder.length;
            HashMap<Integer, Integer> order_map =  new HashMap<Integer, Integer> ();
            
            for (int i = 0; i < len; i++) {
                order_map.put(inorder[i], i);
            }
            
            return helper(inorder, 0, len - 1, postorder, 0, len - 1, order_map);
        }
        
        private TreeNode helper(int[] inorder, int in_low, int in_high, int[] postorder, int post_low, int post_high, HashMap<Integer, Integer> order_map) {
         
         if (in_high < in_low || post_high < post_low) //we use null as base case!!!
            return null;
         
         TreeNode ret = new TreeNode(postorder[post_high]);
         int index = order_map.get(postorder[post_high]);
         
         ret.left = helper(inorder, in_low, index - 1, postorder, post_low, post_low + index - in_low - 1, order_map);
         ret.right = helper(inorder, index + 1, in_high, postorder, post_low + index - in_low, post_high - 1, order_map);   
         
         return ret;
        }
    }
  • 相关阅读:
    Tarjan-割点&桥&双连通
    树状数组
    loli的搜索测试-5
    思维题
    生成树
    贪心
    loli的搜索测试-4
    树链剖分
    基于AHK的上课自动签到
    用列表实现栈
  • 原文地址:https://www.cnblogs.com/airwindow/p/4210508.html
Copyright © 2011-2022 走看看