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
}
相关推荐
是白可可呀3 小时前
LeetCode 169. 多数元素
leetcode
高hongyuan4 小时前
Go语言教程-占位符及演示代码
开发语言·后端·golang
YuTaoShao5 小时前
【LeetCode 热题 100】148. 排序链表——(解法二)分治
java·算法·leetcode·链表
蒟蒻小袁6 小时前
力扣面试150题--全排列
算法·leetcode·面试
緈福的街口7 小时前
【leetcode】2236. 判断根节点是否等于子节点之和
算法·leetcode·职场和发展
祁思妙想8 小时前
【LeetCode100】--- 1.两数之和【复习回滚】
数据结构·算法·leetcode
薰衣草23338 小时前
一天两道力扣(2)
算法·leetcode
chao_7898 小时前
二分查找篇——寻找旋转排序数组中的最小值【LeetCode】
python·线性代数·算法·leetcode·矩阵
风无雨8 小时前
GO启动一个视频下载接口 前端可以边下边放
前端·golang·音视频
zstar-_9 小时前
【算法笔记】6.LeetCode-Hot100-链表专项
笔记·算法·leetcode