题解 | #重建二叉树#

重建二叉树

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
};

全部评论

相关推荐

我的offer呢😡:这不才9月吗,26到明年毕业前能一直找啊,能拿下提前批,转正的,offer打牌的都是有两把刷子的,为什么非要跟他们比。如果别人是9本硕+金牌+好几段大厂实习呢?如果别人是双非通天代呢?如果别人是速通哥呢?,做好自己就行了,我们做不到他们一样提前杀死比赛,但晚点到终点也没啥关系吧
双非应该如何逆袭?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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