输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

二叉树
遍历即将树的所有结点访问且仅访问一次。按照根节点位置的不同分为前序遍历,中序遍历,后序遍历。

前序遍历:根节点->左子树->右子树

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

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

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

import java.util.ArrayList;
import java.util.List;

public class lesson4 {
    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 treeNode = reConstructBinaryTree(pre,in);
        printTreeNode(treeNode);
    }
    private static void printTreeNode(TreeNode treeNode) {
        if(treeNode != null){
            System.out.print(treeNode.val + " ");
            TreeNode left = treeNode.left;
            printTreeNode(left);
            TreeNode right = treeNode.right;
            printTreeNode(right);
        }
    }
    public static TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        print(pre, in);
        TreeNode root = null;
        if(pre != null && pre.length > 0){
            root = new TreeNode(pre[0]);
        }
        //根据中序,找到左边的数组
        if(in != null && in.length > 0){
            List<Integer> inLeftList = new ArrayList();
            List<Integer> inRightList = new ArrayList();
            //分解后序遍历数组
            int[] inLeftChild = null, inRightChild = null;
            boolean find = false;
            for(int i = 0; i < in.length; i ++){
                if(in[i] == pre[0]){//找到了
                    find = true;
                }else if(!find){
                    inLeftList.add(in[i]);
                }else if(find){
                    inRightList.add(in[i]);
                }
            }
            inLeftChild = getIntArray(inLeftList);
            inRightChild = getIntArray(inRightList);
            //分解前序遍历数组
            int[] preLeftChild = null, preRightChild = null;
            int leftCount = 0, rightCount = 0;
            if(inLeftChild != null && inLeftChild.length > 0){
                leftCount = inLeftChild.length;
                if(preLeftChild == null){
                    preLeftChild = new int[leftCount];
                }
            }
            if(inRightChild != null && inRightChild.length > 0){
                rightCount = inRightChild.length;
                if(preRightChild == null){
                    preRightChild = new int[rightCount];
                }
            }
            for(int i = 1; i < pre.length; i ++){
                int value = pre[i];
                if(i <= leftCount){
                    preLeftChild[i - 1] = value;
                }else if(i >= leftCount + 1  && i <= leftCount + rightCount){
                    preRightChild[i - leftCount - 1] = value;
                }
            }
            root.left = reConstructBinaryTree(preLeftChild, inLeftChild);
            root.right = reConstructBinaryTree(preRightChild, inRightChild);
        }
        return root;
    }

    private static void print(int[] pre, int[] in) {
        if(pre == null && in == null)return;
        // TODO Auto-generated method stub
        System.out.print("pre ");
        if(pre != null && pre.length > 0){
            for(int i = 0; i < pre.length; i ++){
                System.out.print(pre[i] + " ");
            }
        }
        System.out.print(" in ");
        if(in != null && in.length > 0){
            for(int i = 0; i < in.length; i ++){
                System.out.print(in[i] + " ");
            }
        }
        System.out.println("");
        System.out.println("-----------------------------------------");
    }
    private static int[] getIntArray(List<Integer> list) {
        // TODO Auto-generated method stub
        if(list != null && list.size() > 0){
            int[] array = new int[list.size()];
            for(int i = 0; i < list.size(); i++){
                array[i] = list.get(i);
            }
            return array;
        }
        return null;
    }
}

打印结果:

pre 1 2 4 7 3 5 6 8  in 4 7 2 1 5 3 8 6 
-----------------------------------------
pre 2 4 7  in 4 7 2 
-----------------------------------------
pre 4 7  in 4 7 
-----------------------------------------
pre 7  in 7 
-----------------------------------------
pre 3 5 6 8  in 5 3 8 6 
-----------------------------------------
pre 5  in 5 
-----------------------------------------
pre 6 8  in 8 6 
-----------------------------------------
pre 8  in 8 
-----------------------------------------
1 2 4 7 3 5 6 8 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值