655. Print Binary Tree

655. Print Binary Tree

题目描述

Print a binary tree in an m*n 2D string array following these rules:

  • The row number m should be equal to the height of the given binary tree.
  • The column number n should always be an odd number.
  • 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 (left-bottom part and right-bottom part). 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.
  • Each unused space should contain an empty string “”.
  • Print the subtrees following the same rules.
1
2
3
4
5
6
7
8
Example 1:
Input:
1
/
2
Output:
[["", "1", ""],
["2", "", ""]]
1
2
3
4
5
6
7
8
9
10
11
Example 2:
Input:
1
/ \
2 3
\
4
Output:
[["", "", "", "1", "", "", ""],
["", "2", "", "", "", "3", ""],
["", "", "4", "", "", "", ""]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Example 3:
Input:
1
/ \
2 5
/
3
/
4
Output:
[["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""]
["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""]
["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""]
["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]

Note: The height of binary tree is in the range of [1, 10].

题目大意

转换二叉树为m * n的二位数组。
规则如下:

  • 行数m为二叉树的深度
  • 列数n必须是以奇数
  • 根节点的值应该放在第一行的中间。以根节点为中轴线,将二维数组分成左右两部分。左下部分输出左子树,右下部分输出右子树,并且左右子树的宽度相同。即使左子树或右子树有一个为nil,也需要为不存在的子树保留其空间。如果左右子树都不存在,则不需要保留任何空间。
  • 每个未使用的空间需要以空字符串表示。
  • 重复以上操作输出子树。

解题思路

1、需先求出树的高度。
2、根据树的高度,求出n。由题目中的例子可知规则,具体看代码实现。
3、递归遍历左右子树,采用双指针,求出节点的下标。

代码

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
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/
class Solution {
func printTree(_ root: TreeNode?) -> [[String]] {
func findDepth(root: TreeNode?) -> Int {
guard let node = root else { return 0 }
return 1 + max(findDepth(root: node.left), findDepth(root: node.right))
}
guard let root = root else { return [] }
let height = findDepth(root: root)
var width = (1 << height) - 1
var ans = Array(repeating: Array(repeating: "", count: width), count: height)
func transform(root: TreeNode?, level: Int, l: Int, r: Int) {
guard let node = root else { return }
let mid = l + (r - l) / 2
ans[level][mid] = "\(node.val)"
transform(root: node.left, level: level + 1, l: l, r: mid - 1)
transform(root: node.right, level: level + 1, l: mid + 1, r: r)
}
transform(root: root, level: 0, l: 0, r: width - 1)
return ans
}
}
0%