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
}
相关推荐
一抹轻笑动人1 天前
Viger笔记
笔记·golang
iAkuya1 天前
(leetcode)力扣100 26环状链表2(双指针)
算法·leetcode·链表
sin_hielo1 天前
leetcode 2402(双堆模拟,小根堆)
数据结构·算法·leetcode
Morwit1 天前
【力扣hot100】 312. 戳气球(区间dp)
c++·算法·leetcode
Q741_1471 天前
C++ 栈 模拟 力扣 394. 字符串解码 每日一题 题解
c++·算法·leetcode·模拟·
雪花desu1 天前
【Hot100-Java简单】:两数之和 (Two Sum) —— 从暴力枚举到哈希表的思维跃迁
java·数据结构·算法·leetcode·哈希表
YGGP1 天前
【Golang】LeetCode 121. 买卖股票的最佳时机
算法·leetcode
YGGP1 天前
【Golang】LeetCode 45. 跳跃游戏 II
算法·leetcode·游戏
YGGP1 天前
【Golang】LeetCode 763. 划分字母区间
算法·leetcode
YGGP1 天前
【Golang】LeetCode 1143. 最长公共子序列
算法·leetcode