class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution_erchashu
{
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root=new TreeNode(pre[0]);//前序的第一个数定为根
int len=pre.length;
//当只有一个数的时候
if(len==1){
root.left=null;
root.right=null;
return root;
}
//找到中序中的根位置
int rootval=root.val;
int i;
for(i=0;i<len;i++){
if(rootval==in[i])
break;
}
//创建左子树
if(i>0){
int[] pr=new int[i];
int[] ino=new int[i];
for(int j=0;j<i;j++){
pr[j]=pre[j+1];
ino[j]=in[j];
}
root.left=reConstructBinaryTree(pr,ino);
}else{
root.left=null;
}
//创建右子树
if(len-i-1>0){
int[] pr=new int[len-i-1];
int[] ino=new int[len-i-1];
for(int j=i+1;j<len;j++){
ino[j-i-1]=in[j];
pr[j-i-1]=pre[j];
}
root.right=reConstructBinaryTree(pr,ino);
}else{
root.right=null;
}
return root;
}
public static void main(String[] args){
int pre[]={1,2,4,7,3,5,6,8};
int in[]={4,7,2,1,5,2,8,6};
Solution_erchashu s=new Solution_erchashu();
TreeNode treeNode=s.reConstructBinaryTree(pre,in);
System.out.println(treeNode.left.val);
System.out.println(treeNode.right.val);
}
}
java实现重建二叉树
最新推荐文章于 2024-05-28 09:38:31 发布