*重建二叉树

二叉树掌握的并不好

转方法:

public class Solution {
    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 startpre, int endpre, int [] in, int startin, int endin) {
        if(startpre > endpre || startin>endin)
            return null;
       
        TreeNode root = new TreeNode(pre[startpre]);
       
        for(int i = startin; i <= endin; i++){
            if(in[i] == pre[startpre]){
                root.left = reConstructBinaryTree(pre, startpre+1, startpre+i-startin, in, startin, i-1);
                root.right = reConstructBinaryTree(pre, i-startin+startpre+1, endpre, in, i+1, endin);
                break;
            }
        }    return root;                                
    }
}

原文地址:https://www.cnblogs.com/dyq19/p/10510802.html