# O(n) time | O(n) space # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: deflargestValues(self, root: Optional[TreeNode]) -> List[int]: if root isNone: return [] queue = collections.deque([root]) ans = [] while queue: l = len(queue) maxi = float('-inf') for i inrange(l): pop_node = queue.popleft() maxi = max(maxi, pop_node.val) if pop_node.left: queue.append(pop_node.left) if pop_node.right: queue.append(pop_node.right) ans.append(maxi) return ans