[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;
    }
}

 

转载于:https://www.cnblogs.com/airwindow/p/4210293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值