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
}
相关推荐
codeejun1 小时前
每日一Go·MySQL-28、账户转账事务设计
开发语言·mysql·golang
Shaoshing2 小时前
LeetCode100 76.最小覆盖字串
java·算法·leetcode
microrain4 小时前
设备说 128.7,平台存了 100
物联网·golang·物模型·sagooiot
Navigator_Z5 小时前
LeetCode //C - 1331. Rank Transform of an Array
c语言·算法·leetcode
Tisfy5 小时前
LeetCode 1202.交换字符串中的元素:分组排序(连通图)
算法·leetcode·字符串·bfs·题解·排序·连通图
利威尔xu14 小时前
Go map 并发十连问:多 goroutine 同时读写触发 fatal error,进程直接崩溃
开发语言·golang
喵了几个咪20 小时前
GoWind Admin 拆解:智能问数——从一句自然语言到一张可信图表
ai·golang·llm·智能问数·gowind
catchadmin21 小时前
Laravel Go 高性能全栈框架发布
golang·php·laravel
小白的高手之路21 小时前
【LeetCode 热题 100】206. 反转链表|链表、迭代、递归(Python)
笔记·python·算法·leetcode·链表·职场和发展·力扣
16月6日-晴1 天前
122. 买卖股票的最佳时机 II【贪心】
算法·leetcode