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

重建二叉树

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-01 11:47
点赞 评论 收藏
分享
CARLJOSEPH...:宝宝你戾气太大了
点赞 评论 收藏
分享
05-12 11:09
已编辑
门头沟学院 后端
已注销:没必要放这么多专业技能的描述。这些应该是默认已会的,写这么多行感觉在凑内容。项目这块感觉再包装包装吧,换个名字,虽然大家的项目基本都是网上套壳的,但是你这也太明显了。放一个业务项目,再放一个技术项目。技术项目,例如中间件的一些扩展和尝试。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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