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
}
相关推荐
xurime7 小时前
Excelize 开源十周年,发布 2.11.0 版本
golang·开源·github·excel·导出·导入·excelize·基础库
YuK.W10 小时前
Leetcode100: 70.爬楼梯、118.杨辉三角、198.打家劫舍
java·算法·leetcode
旖-旎11 小时前
《LeetCode 64 最小路径和 || LeetCode 174 地下城游戏》
c++·算法·leetcode·动态规划
凯瑟琳.奥古斯特13 小时前
力扣1009补码解法C++实现
开发语言·c++·算法·leetcode·职场和发展
凯瑟琳.奥古斯特18 小时前
力扣1008:前序重建BST
开发语言·c++·算法·leetcode·职场和发展
aqiu11111118 小时前
【算法日记 13】LeetCode 49. 字母异位词分组:哈希表的进阶“降维打击”
算法·leetcode·散列表
名字还没想好☜20 小时前
Go 并发实战:用 channel 实现 worker pool
java·数据库·后端·golang·go
人道领域20 小时前
【LeetCode刷题日记】贪心算法理论与实战:455.分发饼干最优解
java·开发语言·数据结构·算法·leetcode·贪心算法
布朗克16821 小时前
Go 入门到精通-09-复合类型之Map
开发语言·后端·golang·map
极***技21 小时前
2026云原生核心!Go高并发限流实战,从原理到落地
开发语言·云原生·golang