题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
还没有通过。。弄了一晚上,我就郁闷了,明明逻辑一样,为什么别人的通过了?即便是几乎是照抄者的,只是改了变量名而已,就不能通过。。。好奇呀,实在没办法了,都零点了,要睡觉了所以就把人家的代码提交上去了///
我的:
public class qweq {
public static void main(String[] args) {
int[] a=new int[]{1,2,3,4,5,6,7};
int[] b=new int[]{3,2,4,1,6,5,7};
Solution s=new Solution();
Solution.TreeNode t=s.reConstructBinaryTree(a, b);
System.out.println(t.val);
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
TreeNode rNode = null;
if(pre==null||in==null)
return null;
if(pre.length==0||in.length==0){
return null;
}
try {
rNode=Constructcore(pre, 0, pre.length-1, in, 0, in.length-1);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
finally{
return rNode;
}
}
private TreeNode Constructcore(int[] pre,int i,int j,int[] in,int m,int n) throws Exception{
int rootValue = pre[i];
TreeNode root=new TreeNode(rootValue);
root.left=null;
root.right=null;
if(i==j){
if(m==n&&pre[i]==pre[m])
return root;
else {
throw new Exception("Invalid input.");
}
}
int k=m;
while (k <= n && in[k] != rootValue) {
k++;
}
if (k > n) {
throw new Exception("Invalid input.");
}
int leftlength=k-m;
int leftpreorderend=i+leftlength;
if(leftlength>0){
root.left=Constructcore(pre, i+1, leftpreorderend, in, m, k-1);
}
if(leftlength<j-i){
root.right=Constructcore(pre, leftpreorderend+1, j, in, k+1, n);
}
return root;
}
}
public class qweq {
public static void main(String[] args) {
int[] a=new int[]{1,2,3,4,5,6,7};
int[] b=new int[]{3,2,4,1,6,5,7};
Solution s=new Solution();
Solution.TreeNode t=s.reConstructBinaryTree(a, b);
System.out.println(t.val);
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
TreeNode rNode = null;
if(pre==null||in==null)
return null;
if(pre.length==0||in.length==0){
return null;
}
try {
rNode=Constructcore(pre, 0, pre.length-1, in, 0, in.length-1);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
finally{
return rNode;
}
}
private TreeNode Constructcore(int[] pre,int i,int j,int[] in,int m,int n) throws Exception{
int rootValue = pre[i];
TreeNode root=new TreeNode(rootValue);
root.left=null;
root.right=null;
if(i==j){
if(m==n&&pre[i]==pre[m])
return root;
else {
throw new Exception("Invalid input.");
}
}
int k=m;
while (k <= n && in[k] != rootValue) {
k++;
}
if (k > n) {
throw new Exception("Invalid input.");
}
int leftlength=k-m;
int leftpreorderend=i+leftlength;
if(leftlength>0){
root.left=Constructcore(pre, i+1, leftpreorderend, in, m, k-1);
}
if(leftlength<j-i){
root.right=Constructcore(pre, leftpreorderend+1, j, in, k+1, n);
}
return root;
}
}
别人的:
public class Solution {
public static void main(String[] args) {
int[] a=new int[]{1,2,3,4,5,6,7};
int[] b=new int[]{3,2,4,1,6,5,7};
Solution s=new Solution();
TreeNode t=s.reConstructBinaryTree(a, b);
System.out.println(t.val);
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode rNode = null;
if (pre == null || in == null || pre.length == 0 || in.length == 0) {
return null;
}
try {
rNode = construct(pre, in, 0, pre.length - 1, 0, in.length - 1);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
return rNode;
}
}
private TreeNode construct(int[] pre, int[] in, int startIndexOfPre,
int endIndexOfPre, int startIndexOfIn, int endIndexOfIn)
throws Exception {
int rootVal = pre[startIndexOfPre];
TreeNode root = new TreeNode(rootVal);
root.left = root.right = null;
if (startIndexOfPre == endIndexOfPre) {
if (endIndexOfIn == startIndexOfIn
&& pre[startIndexOfPre] == in[endIndexOfIn]) {
return root;
} else {
throw new Exception("Invalid input.");
}
}
int rootIndexOfIn = startIndexOfIn;
while (rootIndexOfIn <= endIndexOfIn && in[rootIndexOfIn] != rootVal) {
rootIndexOfIn++;
}
if (rootIndexOfIn > endIndexOfIn) {
throw new Exception("Invalid input.");
}
int leftTreeLength = rootIndexOfIn - startIndexOfIn;
int leftTreePreEndIndex = startIndexOfPre + leftTreeLength;
if (leftTreeLength > 0) {
root.left = construct(pre, in, startIndexOfPre + 1,
leftTreePreEndIndex, startIndexOfIn, rootIndexOfIn - 1);
}
if (leftTreeLength < endIndexOfPre - startIndexOfPre) {
root.right = construct(pre, in, leftTreePreEndIndex + 1,
endIndexOfPre, rootIndexOfIn + 1, endIndexOfIn);
}
return root;
}
}