Meta Data#
Difficulty: medium First Attempt: 2026-10-24
- Submissions: Accepted (first try)
Approach#
BFS level order (Tree Template 2), keeping one maximum per level. Values can be negative (down to -2^31), so each level starts from -inf.
My Attempts#
Accepted on the first try.
Solution#
class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
res = []
queue = deque([root])
while queue:
best = -inf
for _ in range(len(queue)):
node = queue.popleft()
best = max(best, node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
res.append(best)
return res
Complexity#
Time O(n), space O(w).
Mistakes to Avoid#
- Starting each level’s maximum at 0 is wrong when a whole level is negative.
- Return
[]for an empty tree.
Interview Explanation#
I go level by level with BFS and take the maximum of each level starting from -inf, since values can be negative. Time O(n), space is the widest level. A DFS with depth works too: res[depth] = max(res[depth], val).
