首页 > 其他 > 详细

*重建二叉树

时间:2019-03-11 15:34:10      阅读:170      评论:0      收藏:0      [点我收藏+]

二叉树掌握的并不好

转方法:

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!