根据前序和中序遍历重建二叉树

图片说明

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function reConstructBinaryTree(pre, vin)
{
    // write code here
    if(pre.length == 0 || vin.length ==0) return null
    var root = new TreeNode(pre[0])
    var index = vin.indexOf(pre[0])
    root.left = reConstructBinaryTree(pre.slice(1,index+1),vin.slice(0,index))
    root.right = reConstructBinaryTree(pre.slice(index+1),vin.slice(index+1))
    return root
}
树算法 文章被收录于专栏

树相关算法

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-25 17:13
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
快点约我面试吧
投递百度等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务