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
}
相关推荐
Dream it possible!2 小时前
LeetCode 面试经典 150_二叉树_二叉树中的最大路径和(77_124_C++_困难)(DFS)
c++·leetcode·面试·二叉树
做怪小疯子6 小时前
LeetCode 热题 100——子串——和为 K 的子数组
算法·leetcode·职场和发展
希望有朝一日能如愿以偿11 小时前
力扣每日一题:仅含1的子串数
算法·leetcode·职场和发展
q***710812 小时前
【Golang】——Gin 框架中的表单处理与数据绑定
microsoft·golang·gin
苏小瀚12 小时前
算法---FloodFill算法和记忆化搜索算法
数据结构·算法·leetcode
少许极端15 小时前
算法奇妙屋(十二)-优先级队列(堆)
数据结构·算法·leetcode·优先级队列··图解算法
Kuo-Teng17 小时前
LeetCode 118: Pascal‘s Triangle
java·算法·leetcode·职场和发展·动态规划
q***728717 小时前
Golang 构建学习
开发语言·学习·golang
野蛮人6号18 小时前
力扣热题100道之207课程表
算法·leetcode·职场和发展
学学学无无止境18 小时前
力扣-买卖股票的最佳时机
leetcode