Java根据前序、中序遍历创建二叉树和根据中序、后序遍历创建二叉树代码实现(包括前序、中序、后序遍历代码实现)

  • Java根据前序、中序遍历创建二叉树

  • 根据中序、后序遍历创建二叉树代码实现

  • (包括前序、中序、后序遍历代码实现)

import java.util.Scanner;

class TreeNode {//树节点
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

public class Test{
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int[] DLR = new int[] {4, 2, 6, 3, 1, 5};//前序
		int[] LDR = new int[] {6, 2, 3, 4, 1, 5};//中序
		int[] LRD = new int[] {6, 3, 2, 5, 1, 4};//后序
		TreeNode root;
		root = reConstructBinaryTreeByPI(DLR, LDR);//前中建二叉树
		LRD(root);//后序
		System.out.println();
		LDR(root);//中序
		System.out.println();
		root = reConstructBinaryTreeByIO(LRD, LDR);//中后建二叉树
		DLR(root);//前序
		System.out.println();
		LDR(root);//中序
		System.out.println();
	}	
	
	public static TreeNode reConstructBinaryTreeByPI(int [] pre,int [] in) {//前中序创建二叉树
        TreeNode root = reConstructBinaryTreeByPI(pre, 0, pre.length-1, in, 0, in.length-1);
        return root;
	}
	
	public static TreeNode reConstructBinaryTreeByPI(int[] pre, int startPre, int endPre, int[] in, int startIn, int endIn){//pre是前序遍历,in是中序遍历
        if(startPre > endPre || startIn > endIn){
            return null;
        }
        // 根据前序遍历结果,创建根节点
        TreeNode root  = new TreeNode(pre[startPre]);
        // 在中序遍历的结果中找到根节点,找到其左右结点
        //【4】 2 6 3 1 5 - 前
        //6 2 3 【4】 1 5 - 中
        //6 3 2 5 1 【4】 - 后
        for(int i = startIn; i <= endIn; i++){
            if(in[i] == pre[startPre]){
                root.left = reConstructBinaryTreeByPI(pre, startPre+1, startPre + (i - startIn), in, startIn, i-1); //左孩子是中序遍历根节点左子树的根节点
                root.right = reConstructBinaryTreeByPI(pre, startPre + (i-startIn) + 1, endPre, in, i+1, endIn);//右孩子是中序遍历根节点右子树的根节点
            }
        }
        return root;
    }
	
	public static TreeNode reConstructBinaryTreeByIO(int [] out,int [] in) {//中后序创建二叉树
        TreeNode root = reConstructBinaryTreeByIO(out, 0, out.length-1, in, 0, in.length-1);
        return root;
	}
	
	public static TreeNode reConstructBinaryTreeByIO(int[] out, int startOut, int endOut, int[] in, int startIn, int endIn){//out是后序遍历,in是中序遍历
        if(startOut > endOut || startIn > endIn){
            return null;
        }
        // 根据后序遍历结果,创建根节点
        TreeNode root  = new TreeNode(out[endOut]);
        // 在中序遍历的结果中找到根节点,找到其左右结点
        //6 3 2 5 1 【4】 - 后
        //6 2 3 【4】 1 5 - 中
        //【4】 2 6 3 1 5 - 前
        for(int i = startIn; i <= endIn; i++){
            if(in[i] == out[endOut]){
                root.left = reConstructBinaryTreeByIO(out, startOut, endOut - (endIn - i) - 1, in, startIn, i-1); //左孩子是中序遍历根节点左子树的根节点
                root.right = reConstructBinaryTreeByIO(out, endOut - (endIn - i), endOut - 1, in, i + 1, endIn);//右孩子是中序遍历根节点右子树的根节点
            }
        }
        return root;
    }
	
	public static void LRD(TreeNode head) {//后序遍历
		if(head.left != null) LRD(head.left);
		if(head.right != null) LRD(head.right);
		System.out.print(head.val);
	}
	public static void LDR(TreeNode head) {//中序遍历
		if(head.left != null) LDR(head.left);
		System.out.print(head.val);
		if(head.right != null) LDR(head.right);
		
	}
	public static void DLR(TreeNode head) {//前序遍历
		System.out.print(head.val);
		if(head.left != null) DLR(head.left);
		if(head.right != null) DLR(head.right);
	}
}
发布了233 篇原创文章 · 获赞 254 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/weixin_44485744/article/details/105017174
今日推荐