- reConstructBinaryTree的使用
- 使用起来有点像python的索引截取
import java.util.Arrays;
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
if(pre.length==0)
{
return null;
}
//索引查找
int i=0;
for(i=0;i<pre.length;i++)
{
if(pre[0]==vin[i])
{
break;
}
}
int index=i;
//创建子结点
TreeNode root=new TreeNode(pre[0]);
root.left=reConstructBinaryTree(Arrays.copyOfRange(pre,1,1+index),Arrays.copyOfRange(vin,0,index));
root.right=reConstructBinaryTree(Arrays.copyOfRange(pre,1+index,vin.length),Arrays.copyOfRange(vin,index+1,vin.length));
return root;
}
}