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

来源:互联网 发布:iphone和ipad软件同步 编辑:程序博客网 时间:2024/05/16 16:05

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