题解 | #从尾到头打印链表#

重建二叉树

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

alt

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function reConstructBinaryTree(pre, vin)
{   if (!pre.length||!vin.length) return null
    let root=new TreeNode(pre[0])
    let leftSize=vin.indexOf(root.val)
//     let preLeft=pre.slice(1,1+leftSize)
//     let vinLeft=vin.slice(0,leftSize)
//     let preRight=pre.slice(2+leftSize)  //  这里注意是1 很容易搞错 
//     let vinRight=vin.slice(leftSize+1)
    root.left=reConstructBinaryTree(pre.slice(1,1+leftSize),vin.slice(0,leftSize))
    root.right=reConstructBinaryTree(pre.slice(1+leftSize),vin.slice(leftSize+1))
    return root 
    
}
module.exports = {
    reConstructBinaryTree : reConstructBinaryTree
};
看这个图直接秒杀
全部评论

相关推荐

2024-12-27 10:21
已编辑
海南师范大学 媒介策划
到我怀里来:身高体重住址这些就别写了,留几个关键的就行,工作经历突出重点写详细点
点赞 评论 收藏
分享
2024-12-23 11:36
中南大学 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务