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
}
相关推荐
Morwit2 小时前
【力扣hot100】 1. 两数之和
数据结构·c++·算法·leetcode·职场和发展
py有趣2 小时前
力扣热门100题之岛屿的数量(DFS/BFS经典题)
leetcode·深度优先·宽度优先
qinian_ztc3 小时前
frida 14.2.18 安装报错解决
算法·leetcode·职场和发展
Wenweno0o4 小时前
Eino - 错误处理与稳定性
golang·智能体·eino
田梓燊5 小时前
2026/4/11 leetcode 3741
数据结构·算法·leetcode
王码码20355 小时前
Go语言中的Elasticsearch操作:olivere实战
后端·golang·go·接口
Tomhex5 小时前
Go语言import用法详解
golang·go
小肝一下7 小时前
每日两道力扣,day8
c++·算法·leetcode·哈希算法·hot100
Tomhex7 小时前
Golang空白导入的真正用途
golang·go
语戚8 小时前
力扣 51. N 皇后:基础回溯、布尔数组优化、位运算全解(Java 实现)
java·算法·leetcode·力扣·剪枝·回溯·位运算