题目:
题解:
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
}