# 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]: # BFS ifnot root: return [] queue = deque([root]) res = [] while queue: nextLevel = deque([]) localMax = float('-inf') for i in range(len(queue)): front = queue.popleft() localMax = max(localMax, front.val) if front.left: nextLevel.append(front.left) if front.right: nextLevel.append(front.right) res.append(localMax) queue = nextLevel return res
DFS
have an result array, pass depth info
if array length smaller than depth, append current root.val