classSolution: defverticalTraversal(self, root: Optional[TreeNode]) -> List[List[int]]: groups = defaultdict(list) defdfs(root,col,row): if root isNone: return groups[col].append((row,root.val))#每一个列号下添加每一行中该列元素的元组 dfs(root.left,col-1,row+1) dfs(root.right,col+1,row+1) dfs(root,0,0) ans = [] for _,g insorted(groups.items()): g.sort() ans.append([val for _,val in g]) return ans