|
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 -->
|
8 | 8 |
|
9 |
| -<p>在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则:</p> |
| 9 | +<p>给你一棵二叉树的根节点 <code>root</code> ,请你构造一个下标从 <strong>0</strong> 开始、大小为 <code>m x n</code> 的字符串矩阵 <code>res</code> ,用以表示树的 <strong>格式化布局</strong> 。构造此格式化布局矩阵需要遵循以下规则:</p> |
10 | 10 |
|
11 |
| -<ol> |
12 |
| - <li>行数 <code>m</code> 应当等于给定二叉树的高度。</li> |
13 |
| - <li>列数 <code>n</code> 应当总是奇数。</li> |
14 |
| - <li>根节点的值(以字符串格式给出)应当放在可放置的第一行正中间。根节点所在的行与列会将剩余空间划分为两部分(<strong>左下部分和右下部分</strong>)。你应该将左子树输出在左下部分,右子树输出在右下部分。左下和右下部分应当有相同的大小。即使一个子树为空而另一个非空,你不需要为空的子树输出任何东西,但仍需要为另一个子树留出足够的空间。然而,如果两个子树都为空则不需要为它们留出任何空间。</li> |
15 |
| - <li>每个未使用的空间应包含一个空的字符串<code>""</code>。</li> |
16 |
| - <li>使用相同的规则输出子树。</li> |
17 |
| -</ol> |
| 11 | +<ul> |
| 12 | + <li>树的 <strong>高度</strong> 为 <code>height</code> ,矩阵的行数 <code>m</code> 应该等于 <code>height + 1</code> 。</li> |
| 13 | + <li>矩阵的列数 <code>n</code> 应该等于 <code>2<sup>height+1</sup> - 1</code> 。</li> |
| 14 | + <li><strong>根节点</strong> 需要放置在 <strong>顶行</strong> 的 <strong>正中间</strong> ,对应位置为 <code>res[0][(n-1)/2]</code> 。</li> |
| 15 | + <li>对于放置在矩阵中的每个节点,设对应位置为 <code>res[r][c]</code> ,将其左子节点放置在 <code>res[r+1][c-2<sup>height-r-1</sup>]</code> ,右子节点放置在 <code>res[r+1][c+2<sup>height-r-1</sup>]</code> 。</li> |
| 16 | + <li>继续这一过程,直到树中的所有节点都妥善放置。</li> |
| 17 | + <li>任意空单元格都应该包含空字符串 <code>""</code> 。</li> |
| 18 | +</ul> |
18 | 19 |
|
19 |
| -<p><strong>示例 1:</strong></p> |
| 20 | +<p>返回构造得到的矩阵<em> </em><code>res</code> 。</p> |
20 | 21 |
|
21 |
| -<pre> |
22 |
| -<strong>输入:</strong> |
23 |
| - 1 |
24 |
| - / |
25 |
| - 2 |
26 |
| -<strong>输出:</strong> |
27 |
| -[["", "1", ""], |
28 |
| - ["2", "", ""]] |
29 |
| -</pre> |
| 22 | +<p> </p> |
30 | 23 |
|
31 |
| -<p><strong>示例 2:</strong></p> |
| 24 | +<p> </p> |
32 | 25 |
|
| 26 | +<p><strong>示例 1:</strong></p> |
| 27 | +<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0655.Print%20Binary%20Tree/images/print1-tree.jpg" style="width: 141px; height: 181px;" /> |
33 | 28 | <pre>
|
34 |
| -<strong>输入:</strong> |
35 |
| - 1 |
36 |
| - / \ |
37 |
| - 2 3 |
38 |
| - \ |
39 |
| - 4 |
40 |
| -<strong>输出:</strong> |
41 |
| -[["", "", "", "1", "", "", ""], |
42 |
| - ["", "2", "", "", "", "3", ""], |
43 |
| - ["", "", "4", "", "", "", ""]] |
| 29 | +<strong>输入:</strong>root = [1,2] |
| 30 | +<strong>输出:</strong> |
| 31 | +[["","1",""], |
| 32 | + ["2","",""]] |
44 | 33 | </pre>
|
45 | 34 |
|
46 |
| -<p><strong>示例 3:</strong></p> |
47 |
| - |
| 35 | +<p><strong>示例 2:</strong></p> |
| 36 | +<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0655.Print%20Binary%20Tree/images/print2-tree.jpg" style="width: 207px; height: 302px;" /> |
48 | 37 | <pre>
|
49 |
| -<strong>输入:</strong> |
50 |
| - 1 |
51 |
| - / \ |
52 |
| - 2 5 |
53 |
| - / |
54 |
| - 3 |
55 |
| - / |
56 |
| -4 |
57 |
| -<strong>输出:</strong> |
58 |
| -[["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""] |
59 |
| - ["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""] |
60 |
| - ["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""] |
61 |
| - ["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]] |
| 38 | +<strong>输入:</strong>root = [1,2,3,null,4] |
| 39 | +<strong>输出:</strong> |
| 40 | +[["","","","1","","",""], |
| 41 | + ["","2","","","","3",""], |
| 42 | + ["","","4","","","",""]] |
62 | 43 | </pre>
|
63 | 44 |
|
64 |
| -<p><strong>注意:</strong> 二叉树的高度在范围 [1, 10] 中。</p> |
| 45 | +<p> </p> |
| 46 | + |
| 47 | +<p><strong>提示:</strong></p> |
| 48 | + |
| 49 | +<ul> |
| 50 | + <li>树中节点数在范围 <code>[1, 2<sup>10</sup>]</code> 内</li> |
| 51 | + <li><code>-99 <= Node.val <= 99</code></li> |
| 52 | + <li>树的深度在范围 <code>[1, 10]</code> 内</li> |
| 53 | +</ul> |
65 | 54 |
|
66 | 55 | ## 解法
|
67 | 56 |
|
|
0 commit comments