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
}
相关推荐
x_xbx3 小时前
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode
愣头不青7 小时前
96.不同的二叉搜索树
数据结构·算法·leetcode
golang学习记8 小时前
Go 实时批处理:让数据库少挨点打 [特殊字符]
开发语言·数据库·golang
alphaTao9 小时前
LeetCode 每日一题 2026/3/23-2026/3/29
服务器·windows·leetcode
不光头强10 小时前
力扣78子集题解
算法·leetcode·深度优先
Magic--10 小时前
经典概率题:飞机座位分配问题(LeetCode 1227)超详细解析
算法·leetcode·职场和发展
hutengyi11 小时前
go测试问题记录
开发语言·后端·golang
KevinCyao11 小时前
Go短信营销接口示例代码:Golang高并发调用营销短信接口的实现方案与代码分享
android·前端·网络·golang·前端框架
We་ct11 小时前
LeetCode 4. 寻找两个正序数组的中位数:二分优化思路详解
前端·数据结构·算法·leetcode·typescript·二分
精神小伙就是猛12 小时前
使用go-zero快速搭建一个微服务(一)
开发语言·后端·微服务·golang