<jupyter_start><jupyter_text>说明：    在一个 m*n 的二维字符串数组中输出二叉树，并遵守以下规则：    1、行数 m 应当等于给定二叉树的高度。    2、列数 n 应当总是奇数。    3、根节点的值（以字符串格式给出）应当放在可放置的第一行正中间。    根节点所在的行与列会将剩余空间划分为两部分（左下部分和右下部分）。你    应该将左子树输出在左下部分，右子树输出在右下部分。    左下和右下部分应当有相同的大小。即使一个子树为空而另一个非空，你不需要为空的子树输出任何东西，但仍需要为另一个子树留出足够的空间。    然而，如果两个子树都为空则不需要为它们留出任何空间。    每个未使用的空间应包含一个空的字符串""。    使用相同的规则输出子树。    示例 1:    输入:             1            /           2    输出:        [["", "1", ""],         ["2", "", ""]]示例 2:    输入:         1        / \       2   3        \         4    输出:        [["", "", "", "1", "", "", ""],         ["", "2", "", "", "", "3", ""],         ["", "", "4", "", "", "", ""]]示例 3:    输入:                  1                 / \                2   5               /               3              /             4     输出:        [["",  "",  "", "",  "", "", "", "1", "",  "",  "",  "",  "", "", ""]         ["",  "",  "", "2", "", "", "", "",  "",  "",  "",  "5", "", "", ""]         ["",  "3", "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]         ["4", "",  "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]]注意: 二叉树的高度在范围 [1, 10] 中。<jupyter_code>[["","","","","","","","","","","","","","","","1","","","","","","","","","","","","","","",""],
 ["","","","","","","","2","","","","","","","","","","","","","","","","3","","","","","","",""],
 ["","","","4","","","","","","","","5","","","","","","","","","","","","","","","","","","",""],
 ["","6","","","","7","","","","","","","","","","","","","","","","","","","","","","","","",""],
 ["","","8","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]]
a = ["","","","","","","","1","","","","","","",""]
print(len(a))
[["","","","","","","","1","","","","","","",""],
 ["","","2","","","","","","","","3","","","",""],
 ["","","","","5","","6","","","","","","7","4",""],
 ["","10","","12","","11","","","","","8","","","","9"]]

[["","","","","","","","1","","","","","","",""],
 ["","","","2","","","","","","","","3","","",""],
 ["","4","","","","5","","","","6","","","","7",""],
 ["8","","9","","10","","11","","12","","","","","",""]]
from collections import deque

class Solution:
    def printTree(self, root: TreeNode) -> List[List[str]]:
        max_depth = self.get_depth(root)
        max_len = pow(2, max_depth) - 1
        res = [[''] * (pow(2, max_depth) - 1) for _ in range(max_depth)]
        nodes = deque([(root, max_len//2)])
        for i in range(max_depth):
            for _ in range(len(nodes)):
                node, idx = nodes.popleft()
                if node:
                    res[i][idx] = str(node.val)
                    if node.left:
                        nodes.append((node.left, idx - pow(2, max_depth-i-2)))
                    if node.right:
                        nodes.append((node.right, idx + pow(2, max_depth-i-2)))
        return res
        
    
    def get_depth(self, root):
        if not root:
            return 0
        return max(self.get_depth(root.left), self.get_depth(root.right)) + 1
 def get_height(node):
            return 0 if not node else 1 + max(get_height(node.left), get_height(node.right))
        
        def update_output(node, row, left, right):
            if not node:
                return
            mid = (left + right) // 2
            self.output[row][mid] = str(node.val)
            update_output(node.left, row + 1 , left, mid - 1)
            update_output(node.right, row + 1 , mid + 1, right)
            
        height = get_height(root)
        width = 2 ** height - 1
        self.output = [[''] * width for i in range(height)]
        update_output(node=root, row=0, left=0, right=width - 1)
        return self.output<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.3862050456253355, 'fraction_numerical': 0.0249597423510467, 'mean_word_length': 2.391264786169245, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '490370', 'license': 'Apache-2.0', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1520, 'n_words': 195, 'path': 'Tree/1021/655. Print Binary Tree.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '3726'}