Medium
Given the root
of a binary tree, return an array of the largest value in each row of the tree (0-indexed).
Example 1:
Input: root = [1,3,2,5,3,null,9]
Output: [1,3,9]
Example 2:
Input: root = [1,2,3]
Output: [1,3]
Constraints:
[0, 104]
.-231 <= Node.val <= 231 - 1
import com_github_leetcode.TreeNode
import java.util.LinkedList
import java.util.Queue
/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun largestValues(root: TreeNode?): List<Int> {
val list: MutableList<Int> = ArrayList()
val queue: Queue<TreeNode?> = LinkedList()
if (root != null) {
queue.offer(root)
while (queue.isNotEmpty()) {
var max = Int.MIN_VALUE
val size = queue.size
for (i in 0 until size) {
val curr = queue.poll()
max = Math.max(max, curr!!.`val`)
if (curr.left != null) {
queue.offer(curr.left)
}
if (curr.right != null) {
queue.offer(curr.right)
}
}
list.add(max)
}
}
return list
}
}