-
Notifications
You must be signed in to change notification settings - Fork 160
/
print-binary-tree.md
60 lines (53 loc) · 1.93 KB
/
print-binary-tree.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<p>Print a binary tree in an m*n 2D string array following these rules: </p>
<ol>
<li>The row number <code>m</code> should be equal to the height of the given binary tree.</li>
<li>The column number <code>n</code> should always be an odd number.</li>
<li>The root node's value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (<b>left-bottom part and right-bottom part</b>). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don't need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don't need to leave space for both of them. </li>
<li>Each unused space should contain an empty string <code>""</code>.</li>
<li>Print the subtrees following the same rules.</li>
</ol>
<p><b>Example 1:</b><br />
<pre>
<b>Input:</b>
1
/
2
<b>Output:</b>
[["", "1", ""],
["2", "", ""]]
</pre>
</p>
<p><b>Example 2:</b><br />
<pre>
<b>Input:</b>
1
/ \
2 3
\
4
<b>Output:</b>
[["", "", "", "1", "", "", ""],
["", "2", "", "", "", "3", ""],
["", "", "4", "", "", "", ""]]
</pre>
</p>
<p><b>Example 3:</b><br />
<pre>
<b>Input:</b>
1
/ \
2 5
/
3
/
4
<b>Output:</b>
[["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""]
["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""]
["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""]
["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]
</pre>
</p>
<p><b>Note:</b>
The height of binary tree is in the range of [1, 10].
</p>