1class Solution:2def printTree(self, root: TreeNode) -> List[List[str]]:3height = 045def dfs(node, h): # Find height6nonlocal height7height = max(height, h)8if node.left:9dfs(node.left, h + 1)10if node.right:11dfs(node.right, h + 1)1213dfs(root, 0)14n = 2 ** (height + 1) - 1 # Get `n`15offset = (n - 1) // 2 # Column for root node16ans = [[""] * n for _ in range(height + 1)]17q = [(root, 0, offset)]18for i in range(height + 1): # BFS19tmp_q = []20while q:21cur, r, c = q.pop()22ans[r][c] = str(cur.val)23if cur.left:24tmp_q.append((cur.left, r + 1, c - 2 ** (height - r - 1)))25if cur.right:26tmp_q.append((cur.right, r + 1, c + 2 ** (height - r - 1)))27q = tmp_q28return ans