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
}
相关推荐
py有趣5 小时前
力扣热门100题之接雨水
算法·leetcode
不会写DN5 小时前
构建一个抗揍的 Go TCP 聊天服务:异常兜底与防御性编程实践
tcp/ip·golang·php
不会写DN7 小时前
Go中如何跨语言实现传输? - GRPC
开发语言·qt·golang
py有趣8 小时前
力扣热门100题之合并区间
算法·leetcode
派大星~课堂8 小时前
【力扣-138. 随机链表的复制 ✨】Python笔记
python·leetcode·链表
cpp_25018 小时前
P10108 [GESP202312 六级] 闯关游戏
数据结构·c++·算法·动态规划·题解·洛谷·gesp六级
py有趣8 小时前
力扣热门100题之最小覆盖子串
算法·leetcode
北顾笙9809 小时前
day15-数据结构力扣
数据结构·算法·leetcode
人道领域10 小时前
【LeetCode刷题日记:24】两两交换链表
算法·leetcode·链表
北顾笙98010 小时前
day16-数据结构力扣
数据结构·算法·leetcode