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_Z2 小时前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode
Navigator_Z19 小时前
LeetCode //C - 1318. Minimum Flips to Make a OR b Equal to c
c语言·算法·leetcode
驭渊的小故事1 天前
LeetCode 33:搜索旋转排序数组——从二分查找到旋转数组的区间判定
算法·leetcode·职场和发展
利威尔xu1 天前
Go sync.Map底层3层缓存:并发写map直接崩溃,读多写少无锁读拖垮选错锁
golang
郝学胜-神的一滴2 天前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
CoderYanger2 天前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
Navigator_Z2 天前
LeetCode //C - 1323. Maximum 69 Number
c语言·算法·leetcode
利威尔xu2 天前
2026-11-19-Go反射为什么慢面试
开发语言·面试·golang
关键词安全2 天前
PHP / Node.js / Go / Rust 全部中招(CVE-2026-109618、109619、111497、111943)完整排查与修复清单
golang·rust·node.js
Pniubi4 天前
力扣2 . 两数相加
算法·leetcode·职场和发展