import java.util.*; /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ //非常感谢大神啊,java的代码可以参照我的 public class Solution { public TreeNode reConstructBinaryTree(int [] pre,int [] vin) { if(pre.length == 0) return null; TreeNode root = new TreeNode(pre[0]); int index = -1; for(int i = 0;i < vin.length;i++) { if(vin[i] == pre[0]) { index = i; } } int[] pre1 = Arrays.copyOfRange(pre, 1, index+1); int[] pre2 = Arrays.copyOfRange(pre, index+1, pre.length); int[] vin1 = Arrays.copyOfRange(vin, 0, index); int[] vin2 = Arrays.copyOfRange(vin, index+1, vin.length); root.left = reConstructBinaryTree(pre1, vin1); root.right = reConstructBinaryTree(pre2, vin2); return root; } }
点赞

相关推荐

10-13 13:49
南京大学 财务
饿魔:笑死我了,你简直是个天才
点赞 评论 收藏
分享
10-22 19:44
门头沟学院 Java
面了100年面试不知...:那我得去剪个头
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务