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

[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal

时间:2015-12-05 11:07:56      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        return helper(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);
    }
    public TreeNode helper(int[] inorder, int[] postorder, int l1, int r1, int l2, int r2) {
        if (l1 > r1) {
            return null;
        }
        TreeNode root = new TreeNode(postorder[r2]);
        int i = 0;
        for (; i + l1 < r1; i++) {
            if (inorder[i + l1] == root.val) {
                break;
            }
        }
        root.left = helper(inorder, postorder, l1, l1 + i - 1, l2, l2 + i - 1);
        root.right = helper(inorder, postorder, l1 + i + 1, r1, l2 + i, r2 - 1);
        return root;
    }
}

 

[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5021044.html

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