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;
        }
    }
  • 相关阅读:
    线程池的爆掉
    WebApi的调用-1.前端调用
    使用SqlSugar 4.X的T4生成实体类
    JSON.NET 空值处理, 数字转字符,时间格式化
    C#获取路径
    MVC4 下DropDownList使用方法(转)
    14.并发与异步
    14.并发与异步
    14.并发与异步
    VS2013 生成时复制文件或目录到指定目录
  • 原文地址:https://www.cnblogs.com/airwindow/p/4210508.html
Copyright © 2011-2022 走看看