Golang | Leetcode Golang题解之第104题二叉树的最大深度

题目:

题解:

Go 复制代码
func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    queue := []*TreeNode{}
    queue = append(queue, root)
    ans := 0
    for len(queue) > 0 {
        sz := len(queue)
        for sz > 0 {
            node := queue[0]
            queue = queue[1:]
            if node.Left != nil {
                queue = append(queue, node.Left)
            }
            if node.Right != nil {
                queue = append(queue, node.Right)
            }
            sz--
        }
        ans++
    }
    return ans
}
相关推荐
多米Domi0118 小时前
0x3f第33天复习 (16;45-18:00)
数据结构·python·算法·leetcode·链表
Lips6119 小时前
2026.1.16力扣刷题
数据结构·算法·leetcode
今天_也很困11 小时前
LeetCode 热题100-15.三数之和
数据结构·算法·leetcode
千金裘换酒11 小时前
LeetCode 数组经典题刷题
算法·leetcode·职场和发展
alphaTao13 小时前
LeetCode 每日一题 2026/1/12-2026/1/18
python·算法·leetcode
sin_hielo13 小时前
leetcode 2943
数据结构·算法·leetcode
程序员-King.15 小时前
day134—快慢指针—环形链表(LeetCode-141)
算法·leetcode·链表·快慢指针
Swift社区15 小时前
LeetCode 376 摆动序列
算法·leetcode·职场和发展
且去填词15 小时前
深入理解 GMP 模型:Go 高并发的基石
开发语言·后端·学习·算法·面试·golang·go
a程序小傲16 小时前
京东Java面试被问:多活数据中心的流量调度和数据同步
java·开发语言·面试·职场和发展·golang·边缘计算