public class Solution {
int index=0;
private TreeNode reConstructBinaryTree(int [] pre,int [] in,int left,int right){
if(index>=pre.length||left>=right){
return null;
}
int rootIndex=left;
while(rootIndex<right){
if(pre[index]==in[rootIndex]){
break;
}
rootIndex++;
}
TreeNode newRoot=new TreeNode(in[rootIndex]);
index++;
newRoot.left=reConstructBinaryTree(pre,in,left,rootIndex);
newRoot.right=reConstructBinaryTree(pre,in,rootIndex+1,right);
return newRoot;
}
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
return reConstructBinaryTree(pre,in,0,in.length);
}
}
如何根据二叉树的前序遍历结果与中序遍历结果还原二叉树
最新推荐文章于 2023-12-31 23:24:59 发布