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
}
相关推荐
cpp_250125 分钟前
P8597 [蓝桥杯 2013 省 B] 翻硬币
数据结构·c++·算法·蓝桥杯·题解
leoufung1 小时前
LeetCode 63:Unique Paths II - 带障碍网格路径问题的完整解析与面试技巧
算法·leetcode·面试
还不秃顶的计科生1 小时前
力扣hot100第三题:最长连续序列python
python·算法·leetcode
一起养小猫2 小时前
LeetCode100天Day8-缺失数字与只出现一次的数字
java·数据结构·算法·leetcode
梭七y2 小时前
【力扣hot100题】(115)缺失的第一个正数
数据结构·算法·leetcode
一起养小猫4 小时前
LeetCode100天Day9-无重复字符的最长子串与赎金信
java·开发语言·数据结构·leetcode
努力学算法的蒟蒻4 小时前
day52(1.2)——leetcode面试经典150
算法·leetcode·面试
java修仙传4 小时前
力扣hot100:字符串解码
算法·leetcode·职场和发展
梭七y5 小时前
【力扣hot100题】(116)矩阵置零
算法·leetcode·矩阵