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
}
相关推荐
吃着火锅x唱着歌1 小时前
LeetCode 668.乘法表中第k小的数
算法·leetcode·职场和发展
十八岁讨厌编程3 小时前
【算法训练营 · 补充】LeetCode Hot100(上)
算法·leetcode
浮灯Foden3 小时前
算法-每日一题(DAY18)多数元素
开发语言·数据结构·c++·算法·leetcode·面试
小欣加油3 小时前
leetcode 844 比较含退格的字符串
算法·leetcode·职场和发展
如竟没有火炬4 小时前
全排列——交换的思想
开发语言·数据结构·python·算法·leetcode·深度优先
九江Mgx5 小时前
使用 Go + govcl 实现 Windows 资源管理器快捷方式管理器
windows·golang·govcl
李辰洋5 小时前
go tools安装
开发语言·后端·golang
九江Mgx5 小时前
深入理解 Windows 全局键盘钩子(Hook):拦截 Win 键的 Go 实现
golang·windowshook
wanfeng_095 小时前
go lang
开发语言·后端·golang
绛洞花主敏明5 小时前
go build -tags的其他用法
开发语言·后端·golang