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
}
相关推荐
Navigator_Z7 小时前
LeetCode //C - 1278. Palindrome Partitioning III
c语言·算法·leetcode
lvwangshu8 小时前
登录日志 题解
数据结构·线段树·题解·技巧·性质题·思维好题·细节题
青山木10 小时前
Hot 100 --- 编辑距离
java·数据结构·算法·leetcode·动态规划
All for pursuit.13 小时前
【回溯-4】46.全排列
数据结构·c++·算法·leetcode
大侠归来16 小时前
C 与 Go 语言对比:从设计哲学到工程实践
java·c语言·golang
All for pursuit.17 小时前
【回溯-5】78.子集
数据结构·c++·算法·leetcode
小小龙学IT17 小时前
Go 语言内存管理与垃圾回收(GC)深度解析:从逃逸分析到性能调优适
golang·go1.19
漂流瓶jz18 小时前
UVA-1610 聚会游戏 题解答案代码 算法竞赛入门经典第二版
数据结构·c++·算法·题解·aoapc·算法竞赛入门经典·uva
代码什么用18 小时前
LeetCode-560.和为 K 的子数组
算法·leetcode
All for pursuit.18 小时前
【回溯-3】39.组合总和
数据结构·c++·算法·leetcode