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
}
相关推荐
Kuo-Teng1 天前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
码上淘金1 天前
在 YAML 中如何将 JSON 对象作为字符串整体赋值?——兼谈 Go Template 中的 fromJson 使用
java·golang·json
橘颂TA1 天前
【剑斩OFFER】算法的暴力美学——点名
数据结构·算法·leetcode·c/c++
愚润求学2 天前
【动态规划】专题完结,题单汇总
算法·leetcode·动态规划
·白小白2 天前
力扣(LeetCode) ——43.字符串相乘(C++)
c++·leetcode
小生凡一2 天前
图解|Go语言实现 Agent|LLM+MCP+RAG
开发语言·后端·golang
pipip.2 天前
Go原生高性能内存网关IMS,比Redis更快
开发语言·redis·golang
一匹电信狗2 天前
【C++11】Lambda表达式+新的类功能
服务器·c++·算法·leetcode·小程序·stl·visual studio
在等晚安么2 天前
力扣面试150题打卡
算法·leetcode·面试
q***06292 天前
环境安装与配置:全面了解 Go 语言的安装与设置
开发语言·后端·golang