重构二叉树-------------------采用递归

先序遍历  根节点---左子树---右子树

中序遍历 左子树---根节点----右子树

后序遍历  左子树---右子树---根节点

知道先序序列和中序序列,重构二叉树

1.首先定义树结构的类,一个 TreeNode实例对象是一棵树,存放根节点对应的值,以及左子树和右子树

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

2.根据先序遍历确定根节点,找到根节点在中序遍历的位置,则前面的是左子树,后面的右子树,分别对左子树和右子树采用递归,分别把左子树和右子树的先序遍历和中序遍历传给递归函数

package leecode.programing;

import java.util.Arrays;

public class reConstuctTree {
	public  TreeNode reConstructBinaryTree(int[] pre,int[] in){
        //调用重载方法
        TreeNode root = reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
    
        return root;
    }
    public  TreeNode reConstructBinaryTree(int[] pre,int preStart,int preEnd,int[] in,int inStart,int inEnd){
        if(preStart > preEnd || inStart > inEnd)
            return null;
        TreeNode root = new TreeNode(pre[preStart]);
        for(int i=inStart;i<=inEnd;i++){
            if(in[i] == pre[preStart]){
                root.left = reConstructBinaryTree(pre,preStart+1,preStart+i,in, inStart,i-1);
                root.right = reConstructBinaryTree(pre,i-inStart+preStart+1,preEnd,in,i+1,inEnd);
            }
        }
        System.out.print(root.val);
        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,3,8,6};
		TreeNode root=new reConstuctTree().reConstructBinaryTree(pre,in);	
		System.out.print(root.val);
	}
}

 

知道后序序列和中序序列,重构二叉树

后序序列最后一个节点是根节点,然后递归

package leecode.programing;

public class reTree {
	public  TreeNode reConstructBinaryTree2(int[] in,int[] last){
        TreeNode root = reConstructBinaryTree2(in,0,in.length-1,last,0,last.length-1);
       // System.out.print(root.val);
        return root;
    }
    public  TreeNode reConstructBinaryTree2(int[] in,int inStart,int inEnd,int[] last,int lastStart,int lastEnd){
        if(inStart > inEnd || lastStart > lastEnd)
            return null;
        TreeNode treeNode = new TreeNode(last[lastEnd]);
        for(int i=inStart;i<=inEnd;i++){
            if(in[i] == last[lastEnd]){
                treeNode.left = reConstructBinaryTree2(in, inStart,    i-1,    last,   lastStart,              lastStart+i-inStart-1);
                treeNode.right = reConstructBinaryTree2(in,    i+1,    inEnd,  last,   lastStart+i-inStart,    lastEnd-1);
            }
        }
        
        return treeNode;
    }
   
	public static void main(String[] args) {
		int pre[]={1,2,4,7,3,5,6,8};
		int in[]={4,7,2,1,5,3,8,6};
		TreeNode root=new reTree().reConstructBinaryTree2(pre,in);	
		System.out.print(root.val);

	}

}

二叉树的三种遍历

 //后序
    public void PrintBinaryTreeBacRecur(TreeNode root)
    {
        if (root!=null) 
        {
            PrintBinaryTreeBacRecur(root.left);
            PrintBinaryTreeBacRecur(root.right);
            System.out.print(root.val);
        }
    }
    //先序
    public void PrintBinaryTreePreRecur(TreeNode root)
    {
        if (root!=null) 
        {
            System.out.print(root.val);
            PrintBinaryTreePreRecur(root.left);
            PrintBinaryTreePreRecur(root.right);
        }
    }
    //中序
    public void PrintBinaryTreeMidRecur(TreeNode root)
    {
        if (root!=null) 
        {
            PrintBinaryTreeMidRecur(root.left);
            System.out.print(root.val);
            PrintBinaryTreeMidRecur(root.right);
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值