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

二叉树的最大深度

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
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 14:08
点赞 评论 收藏
分享
07-03 16:02
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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