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
}
相关推荐
文丑颜不良啊10 分钟前
LeetCode2461~2480题目解析
java·leetcode
文丑颜不良啊1 小时前
LeetCode2441~2460题目解析
java·leetcode
文丑颜不良啊2 小时前
LeetCode2521~2540题目解析
java·leetcode
文丑颜不良啊5 小时前
LeetCode2501~2520题目解析
java·leetcode
Pniubi17 小时前
力扣54.螺旋矩阵
算法·leetcode·矩阵
利威尔xu1 天前
Go map 底层崩溃:3 个 goroutine 并发写同一张 map,fatal error 崩掉整个服务
golang·源码·map·并发·后端开发
Rain的Java大神之路1 天前
LeetCode 37 解数独面试攻略:回溯 + 位掩码 + MRV,从暴力到毫秒级求解
java·后端·leetcode
Pniubi1 天前
力扣73 矩阵置零
算法·leetcode·矩阵
文丑颜不良啊2 天前
LeetCode2261~2280题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2241~2260题目解析
java·leetcode