首页 > 其他 > 详细

106. Construct Binary Tree from Inorder and Postorder Traversal

时间:2017-09-29 13:15:37      阅读:264      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!