剑指Offer+第6题+重建二叉树+java

思路:先找出根节点,然后利用递归方法构造二叉树

代码实现

import java.util.Arrays;

class TreeNode{
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x){val = x;}
}
public class Offer06 {

	public TreeNode reConstructBinaryTree(int [] pre, int []in){
		if(pre == null || in == null)
			return null;
		if(pre.length == 0 || in.length == 0)
			return null;
		if(pre.length != in.length)
			return null;
		TreeNode root = new TreeNode(pre[0]);
		for(int i = 0;i<pre.length;i++){
			if(pre[0] == in[i]){
				root.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i+1),
						Arrays.copyOfRange(in, 0, i));
				root.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i+1, pre.length),
						Arrays.copyOfRange(in, i+1, in.length));
			}
		}
		return root;
	}

	//方法2
	public TreeNode reConstructBinaryTree_2(int [] pre,int [] in) {
		TreeNode root = reConstructBinaryTree_2(pre, 0, pre.length-1, in, 0, in.length-1);
		return root;
	}

	public TreeNode reConstructBinaryTree_2(int [] pre, int startPre, int endPre,
											int [] in, int startIn, int endIn){
		if(startPre>endPre || startIn>endIn)
			return null;
		TreeNode root = new TreeNode(pre[startPre]);
		for(int i = startIn;i<=endIn;i++){
			if(pre[startPre] == in[i]){
				//root.left = reConstructBinaryTree_2(pre, startPre+1, startPre+i, in, startIn, i-1);
				//root.right = reConstructBinaryTree_2(pre, startPre+i-1,endPre, in, i+1, endIn);这样写报错,数组越界
				root.left = reConstructBinaryTree_2(pre, startPre+1, startPre+(i-startIn),
						in, startIn, i-1);
				root.right = reConstructBinaryTree_2(pre, startPre+(i-startIn)+1,endPre,
						in, i+1, endIn);
                /*i-starin是在计算当前节点左子树节点个数;i-starin+starpre是确定当前节点左子树在pre里的位置;根据中序遍历+前序遍历的特点,
                pre[starpre+1]就是当前节点的左子节点,pre[i-starin+starpre+1]就是当前节点的右子节点;巧妙的是那个判定条件,越界即左右子节点不存在
                */
			}
		}
		return root;
	}

	public static void main(String[] args) {
		Offer06 of6 = new Offer06();
		//测试用例
		//1,普通二叉树(完全二叉树,不完全二叉树)
		int[] pre1 = {1,2,4,5,3,6,7};
		int[] in1 = {4,2,5,1,6,3,7};
		//System.out.println(of6.reConstructBinaryTree(pre1, in1).val);
		System.out.println(of6.reConstructBinaryTree_2(pre1, in1).val);
		
		int[] pre2 = {1,2,4,7,3,5,6,8};
		int[] in2 = {4,7,2,1,5,3,8,6};
		//System.out.println(of6.reConstructBinaryTree(pre2, in2).val);
		
		//2,特殊二叉树(所有节点都没有右子节点的二叉树,所有节点都没有左子节点的二叉树,只有一个节点的二叉树)
		int[] pre3 = {1,2,4,5};
		int[] in3 = {4,2,5,1};
		//System.out.println(of6.reConstructBinaryTree(pre3, in3).val);
		
		int[] pre4 = {1,3,6,7};
		int[] in4 = {1,6,3,7};
		//System.out.println(of6.reConstructBinaryTree(pre4, in4).val);
		
		int[] pre5 = {1};
		int[] in5 = {1};
		//System.out.println(of6.reConstructBinaryTree(pre5, in5).val);
		
		//3,特殊输入测试(二叉树的根节点为NULL,输入的前序遍历序列和中序遍历序列不匹配)
		int[] pre6 = null;
		int[] in6 = null;
		//System.out.println(of6.reConstructBinaryTree(pre6, in6));

	}
}

输出结果:

牛客网 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值