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
}
相关推荐
文丑颜不良啊15 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z16 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
文丑颜不良啊17 小时前
LeetCode2161~2180题目解析
java·leetcode
JWASX19 小时前
【Go】map 源码解析 - group
后端·golang·map·源码分析
文丑颜不良啊19 小时前
LeetCode2141~2160题目解析
java·leetcode
Dream it possible!1 天前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊1 天前
算法之双指针系列(一)
算法·leetcode
Pniubi2 天前
力扣560.和为k的子数组
算法·leetcode·职场和发展
Pniubi2 天前
力扣438.找到字符串中所有字母异位词
算法·leetcode·职场和发展
联犀2 天前
纯 Go 的 CAD 开源库 go-cad:直接解析 DWG/DXF,现在也能导出 PDF
开发语言·golang·pdf