题解 | #重建二叉树#

重建二叉树

https://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function reConstructBinaryTree(pre, vin)
{
    // write code here
    if (pre.length ===0) return null
    const rootValue = pre[0]
    const vinRootIndex = vin.indexOf(rootValue)
    const leftVin = vin.slice(0, vinRootIndex)
    const rightVin = vin.slice(vinRootIndex + 1)
    const leftPre = pre.slice(1, leftVin.length + 1)
    const rightPre = pre.slice(leftVin.length + 1)
    const root = new TreeNode(rootValue)
    root.left = reConstructBinaryTree(leftPre, leftVin)
    root.right = reConstructBinaryTree(rightPre, rightVin)
    return root
}

module.exports = {
    reConstructBinaryTree : reConstructBinaryTree
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 11:31
点赞 评论 收藏
分享
05-12 17:00
门头沟学院 Java
king122:你的项目描述至少要分点呀,要实习的话,你的描述可以使用什么技术,实现了什么难点,达成了哪些数字指标,这个数字指标尽量是真实的,这样面试应该会多很多,就这样自己包装一下,包装不好可以找我,我有几个大厂最近做过的实习项目也可以包装一下
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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