Golang | Leetcode Golang题解之第515题在每个树行中找最大值

题目:

题解:

Go 复制代码
func largestValues(root *TreeNode) (ans []int) {
    if root == nil {
        return
    }
    q := []*TreeNode{root}
    for len(q) > 0 {
        maxVal := math.MinInt32
        tmp := q
        q = nil
        for _, node := range tmp {
            maxVal = max(maxVal, node.Val)
            if node.Left != nil {
                q = append(q, node.Left)
            }
            if node.Right != nil {
                q = append(q, node.Right)
            }
        }
        ans = append(ans, maxVal)
    }
    return
}

func max(a, b int) int {
    if b > a {
        return b
    }
    return a
}
相关推荐
Nil20810 小时前
leetcode 5最长回文子串
算法·leetcode·职场和发展
m0_6745719710 小时前
力扣 hot 100 python
python·算法·leetcode
圣保罗的大教堂11 小时前
leetcode 3727. 最大交替平方和 中等
leetcode
Xin77012 小时前
LeetCode 200. 岛屿数量(DFS 深度优先遍历法)
leetcode
alphaTao18 小时前
LeetCode 每日一题 2026/10/12-2026/10/18
算法·leetcode
Cccp.12319 小时前
【leetcode】(十三)大数据处理
数据结构·算法·leetcode
Pniubi19 小时前
力扣70爬楼梯
数据结构·算法·leetcode
云和数据.ChenGuang20 小时前
Golang AI 工程师路线图:从入门到实战
开发语言·人工智能·后端·golang
报错小能手21 小时前
内存管理 垃圾回收面试题
golang
Navigator_Z1 天前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode