根据前序遍历中序遍历求二叉树

来源:互联网 发布:烟台华商网络怎么样 编辑:程序博客网 时间:2024/04/30 01:57
    public TreeNode ConstructBinaryTree(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){        TreeNode root=new TreeNode(pre[prestart]);        if(prestart>preend||instart>inend){            return null;        }        for(int i=instart;i<=inend;i++){            if(pre[prestart]==in[i]){                root.left=reConstructBinaryTree(pre,prestart+1,prestart+i-instart,                        in,instart,i-1);                root.right=reConstructBinaryTree(pre,prestart+i-instart+1,preend,                        in,i+1,inend);                break;            }        }        return root;    }
0 0