题解 | #删除链表的倒数第n个节点#

重建二叉树

http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6

递归重建树

public TreeNode reConstructBinaryTree(int [] pre, int [] vin) {
        if (pre == null || pre.length == 0){
            return null;
        }
        TreeNode root = new TreeNode(pre[0]);
        int index = 0;
        for (int i = 0; i < vin.length; i++) {
            if (vin[i] == pre[0]){
                index = i;
            }
        }
        int[] leftPre = Arrays.copyOfRange(pre, 1, index+1);
        int[] rightPre = Arrays.copyOfRange(pre, index+1, pre.length);
        int[] leftVin = Arrays.copyOfRange(vin, 0, index);
        int[] rightVin = Arrays.copyOfRange(vin, index+1, vin.length);
        root.leftChild = reConstructBinaryTree(leftPre, leftVin);
        root.rightChild = reConstructBinaryTree(rightPre, rightVin);
        return root;
    }
全部评论

相关推荐

代码飞升_不回私信人...:啊喂笨蛋算法为什么写查找,线程池怎么放计网上去了,写动态规划真的不会被狠狠地制裁吗oi
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务