剑指offer-重建二叉树

题目描述

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{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 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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值