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

重建二叉树

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
};
看这个图直接秒杀
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务