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

图片说明

/* 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
}
树算法 文章被收录于专栏

树相关算法

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务