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
}
相关推荐
Scabbards_12 小时前
面试Leetcode - Heap 堆
java·leetcode·面试
ValhallaCoder15 小时前
Leetcode-hot100(2026.08.17)
python·算法·leetcode
xcLeigh16 小时前
Go入门:无类型常量与类型常量的区别
服务器·开发语言·golang
泡沫冰@16 小时前
GO 语言基础
开发语言·算法·golang
青 春 记 忆20 小时前
LeetCode 104. 二叉树的最大深度|Python 解法详解
python·算法·leetcode
骇客野人21 小时前
电商商城微服务架构设计方案(SpringCloud/Go+Vue3+MySQL+ES+RocketMQ)
spring cloud·微服务·golang
码农大叔的博客21 小时前
golang示例:for九九乘法表
开发语言·算法·golang
圣殿骑士-Khtangc1 天前
Go-sync-Pool对象池最佳实践与源码分析
golang
互联网中的一颗神经元1 天前
01. Go 内存管理全景架构
java·jvm·golang
有脚就行1 天前
第24篇-Go-gRPC推理服务-高性能跨语言通信
开发语言·人工智能·后端·golang