标签:style ons turn 结果 return tree private || 返回
重建二叉树
1 public class Solution { 2 public TreeNode reConstructBinaryTree(int [] pre, int [] in) { 3 TreeNode root = help(pre,in,0,pre.length-1,0,in.length-1); 4 return root; 5 } 6 private TreeNode help(int[] pre, int[] in,int prebeg,int preend,int inbeg,int inend ){ 7 if(prebeg>preend||inbeg>inend) 8 return null; 9 TreeNode root = new TreeNode(pre[prebeg]); 10 int index = 0; 11 for(int i=0;i<in.length;i++){ 12 if(in[i]==pre[prebeg]){ 13 index = i; 14 break; 15 } 16 } 17 root.left = help(pre,in,prebeg+1,prebeg+(index-inbeg),inbeg,index-1); 18 root.right = help(pre,in,prebeg+(index-inbeg)+1,preend,index+1,inend); 19 return root; 20 } 21 }
标签:style ons turn 结果 return tree private || 返回
原文地址:https://www.cnblogs.com/ztqup666/p/9233325.html