题解:基于递归 | #二叉树根节点到叶子节点的所有路径和#

重建二叉树

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



import java.util.*;
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    int[] pre;
    int[] vin;
    int p;//记录pre中,已经被处理到的节点的索引
    
    public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
        this.pre = pre;
        this.vin = vin;
        p=0;
        return reConstructBinaryTree(0,pre.length-1);
    }
    public TreeNode reConstructBinaryTree(int start,int end) {
        for(int i=start;i<=end;i++){
            if(vin[i]==pre[p]){
                TreeNode node=new TreeNode(pre[p]);
                p++;
                node.left=reConstructBinaryTree(start,i-1);
                node.right=reConstructBinaryTree(i+1,end);
                return node;
            }
        }
        return null;
    }
}
全部评论

相关推荐

07-15 18:09
门头沟学院 Java
点赞 评论 收藏
分享
05-29 20:34
门头沟学院 C++
KarlAllen:得做好直接春招的准备。学历差的话,一是面试要求会比学历好的严格不少,二是就算面试通过了也会被排序。总之暑期和秋招对于学历差的就是及其不友好
无实习如何秋招上岸
点赞 评论 收藏
分享
陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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