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

    The problem:

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

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

    My analysis:

    The idea behind this problem is really elegant and meaningful!
    We need to combinely use both preorder and inorder arrays.
    Rucursion:
    The first node in preorder[] must be the root of preorder array.
    Use this information, we could separate the inorder[] into left sub-tree and right sub-tree.

    One important thing we need to hold is that, we should guarantee the elements of preorder array and inorder array are the same!
    How to guarantee it?
    we should use the connection between preorder and inorder traversal.
    prorder series: ABCDEFGH
    inorder series: BCDAEFGH
    from the preorder series, we could conclude that A is the root of those nodes. from the inorder series, we could conclude that BCD are the nodes belonged to left sub-tree, and EFGH are the nodes belonged to the right sub-tree.
    Even though we can't assure the exeact poistion of BCD in the left sub-tree, we can assure that they must appear ahead of all nodes in the right sub-tree. Thus BCD must appear ahead in the preorder series before EFGH. (the left sub-tree elements must appear before right sub-tree elements in both prorder series and inorder series)

    By using the above inference, we could guarantee we can get the same set of elements both in preorder series and inorder series.

    [inorder_low, index - 1]   [index + 1, high]
    we could infer the sections in preorder series.
    preorder_high - preorder_low - 1 = index - 1 - inorder_low ====> preoder_high = preorder_low + index - inorder_low

    (the elements in the left sub-tree must equal to elements both in inorder series and preorder series)
    note: In preorder series, we chop off the first element. while in inorder series, we chop off the index(th) element.
    left sub-trees preorder series:

    [preorder_low + 1, preorder_low + index - inorder_low]
    
    right sub-trees preorder series:
    [preorder_low + index - inorder_low + 1, preorder_high]

    What an interesting question !

    My solution:

    public class Solution {
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            
            if (preorder == null || preorder.length == 0)
                return null;
            if (inorder == null || inorder.length == 0)
                return null; 
                
            int len = inorder.length;
            HashMap<Integer, Integer> order_map = new HashMap<Integer, Integer>();
            
            for (int i = 0; i < inorder.length; i++ ) {
                order_map.put(inorder[i], i);
            }
            
            return helper(preorder, 0, len - 1, inorder, 0, len - 1, order_map);
        }
        
        private TreeNode helper(int[] preorder, int pre_low, int pre_high, int[] inorder, int in_low, int in_high, HashMap<Integer, Integer> order_map) {
            
            if (pre_low > pre_high || in_low > in_high)
                return null;
                
            TreeNode ret = new TreeNode(preorder[pre_low]);
            int index = order_map.get(preorder[pre_low]);
            
            ret.left = helper(preorder, pre_low + 1, index - in_low  + pre_low, inorder, in_low, index - 1, order_map);
            ret.right = helper(preorder, index - in_low + pre_low + 1, pre_high, inorder, index + 1, in_high, order_map);
            
            return ret;
        }
    }
  • 相关阅读:
    mybatis批量插入数据
    oracle的dmp数据文件的导出和导入以及创建用户
    maven安装第三方jar包到本地仓库
    IntelliJ IDEA 注册码,激活
    分布式事务实现-Spanner
    Redis Cluster原理
    twemproxy源码分析
    Paxos可容错的一致性协议
    UpdateServer事务实现机制
    Coroutine及其实现
  • 原文地址:https://www.cnblogs.com/airwindow/p/4210293.html
Copyright © 2011-2022 走看看