Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

二叉树的最大深度

Sunny-117 opened this issue · comments

commented
二叉树的最大深度
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function(root) {
    if(!root) return 0;
    let res = 0;
    const queue = [root];

    while(queue.length){
        let len = queue.length;
        while(len--){
            let node = queue.shift();
            node.left && queue.push(node.left)
            node.right && queue.push(node.right)
        }
        res++
    }
    return res;
};