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

106. Construct Binary Tree from Inorder and Postorder Traversal

时间:2017-09-29 13:22:01      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:order   nod   node   new t   new   length   blog   binary   tor   

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

  

106. Construct Binary Tree from Inorder and Postorder Traversal

标签:order   nod   node   new t   new   length   blog   binary   tor   

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

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