• [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;
        }
    }
  • 相关阅读:
    【Unity3d】Ray射线初探-射线的原理及用法
    unity3d 的Quaternion.identity和transform.rotation区别是什么
    HOLOLENS的空间管理
    vector3.forward和transform.forward的区别!
    voxel 与 pixel
    图文详解Unity3D中Material的Tiling和Offset是怎么回事
    手机开启HDR后拍照有什么不同?
    什么是UV?
    积和式Permanent在Mathematica中的运算
    [再寄小读者之数学篇](2014-11-20 计算二重积分)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4210293.html
Copyright © 2020-2023  润新知