题解 | #二叉树的最大深度#

二叉树的最大深度

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

采用层序遍历

function maxDepth( root ) {
   if(!root) return 0
    let res = []
    let queue = []
    queue.push(root)
    let num = 0
    
    while(queue.length){
        let len = queue.length
        num++
        for(let i = 0; i < len; i++){
            let node = queue.shift()
            if(node.left) queue.push(node.left)
            if(node.right) queue.push(node.right)
        }
    }
    
    return num
}

全部评论

相关推荐

04-17 18:32
门头沟学院 Java
野猪不是猪🐗:他跟你一个学校,你要是进来之后待遇比他好,他受得了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务