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
}
相关推荐
漂流瓶jz7 小时前
UVA-1442 洞穴 题解答案代码 算法竞赛入门经典第二版
c++·算法·题解·aoapc·算法竞赛入门经典·uva
圣保罗的大教堂7 小时前
leetcode 835. 图像重叠 中等
leetcode
五彩小白11 小时前
GO语言装饰器语法
golang
wabs66611 小时前
关于二叉树【力扣572.另一棵树的子树的思考】
数据结构·c++·算法·leetcode·二叉树
hanlin0311 小时前
刷题笔记:力扣第41题-缺失的第一个正数
笔记·算法·leetcode
0+11114 小时前
算法 --滑动窗口
c++·算法·leetcode
木井巳15 小时前
【记忆化搜索】最长递增子序列
java·算法·leetcode·深度优先·剪枝·推荐算法
圣保罗的大教堂17 小时前
leetcode 1401. 圆和矩形是否有重叠 中等
leetcode
金金计较.17 小时前
Go语言-4
开发语言·golang
hanlin0317 小时前
刷题笔记:力扣第76题-最小覆盖子串
笔记·算法·leetcode