码迷,mamicode.com
首页 > 其他好文 > 详细

105. Construct Binary Tree from Preorder and Inorder Traversal

时间:2017-09-29 12:42:31      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:length   rsa   highlight   new   ons   public   solution   from   uil   

public class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length);
	}
    private TreeNode buildTree(int[] preorder, int l1, int r1, int[] inorder, int l2, int r2){
        if(l1>r1||l2>r2)
            return null;
        int idx=l2;
        while(inorder[idx]!=preorder[l1])
            idx++;
        TreeNode node=new TreeNode(preorder[l1]);
        node.left=buildTree(preorder, l1+1, l1+(idx-l2), inorder, l2, idx-1);
        node.right=buildTree(preorder, l1+(idx-l2)+1, r1, inorder, idx+1, r2);
        return node;
    }
}

  

105. Construct Binary Tree from Preorder and Inorder Traversal

标签:length   rsa   highlight   new   ons   public   solution   from   uil   

原文地址:http://www.cnblogs.com/asuran/p/7610496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!