Skip to content

Commit e012801

Browse files
committed
feat: add solutions to lc problem: No.0655
No.0655.Print Binary Tree
1 parent 1f54d3e commit e012801

File tree

8 files changed

+507
-101
lines changed

8 files changed

+507
-101
lines changed

solution/0600-0699/0655.Print Binary Tree/README.md

+164
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,186 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
> 中文题意不容易读懂,可以参考[英文版本的题目描述](/solution/0600-0699/0655.Print%20Binary%20Tree/README_EN.md)
71+
72+
先求二叉树的高度 h,然后根据 h 求得结果列表的行数和列数 m, n。
73+
74+
根据 m, n 初始化结果列表,然后 dfs 遍历二叉树,依次在每个位置填入二叉树节点值(字符串形式)即可。
75+
7076
<!-- tabs:start -->
7177

7278
### **Python3**
7379

7480
<!-- 这里可写当前语言的特殊实现逻辑 -->
7581

7682
```python
83+
# Definition for a binary tree node.
84+
# class TreeNode:
85+
# def __init__(self, val=0, left=None, right=None):
86+
# self.val = val
87+
# self.left = left
88+
# self.right = right
89+
class Solution:
90+
def printTree(self, root: TreeNode) -> List[List[str]]:
91+
def height(root):
92+
if root is None:
93+
return -1
94+
return 1 + max(height(root.left), height(root.right))
95+
96+
def dfs(root, r, c):
97+
if root is None:
98+
return
99+
ans[r][c] = str(root.val)
100+
dfs(root.left, r + 1, c - 2 ** (h - r - 1))
101+
dfs(root.right, r + 1, c + 2 ** (h - r - 1))
77102

103+
h = height(root)
104+
m, n = h + 1, 2 ** (h + 1) - 1
105+
ans = [[""] * n for _ in range(m)]
106+
dfs(root, 0, (n - 1) // 2)
107+
return ans
78108
```
79109

80110
### **Java**
81111

82112
<!-- 这里可写当前语言的特殊实现逻辑 -->
83113

84114
```java
115+
/**
116+
* Definition for a binary tree node.
117+
* public class TreeNode {
118+
* int val;
119+
* TreeNode left;
120+
* TreeNode right;
121+
* TreeNode() {}
122+
* TreeNode(int val) { this.val = val; }
123+
* TreeNode(int val, TreeNode left, TreeNode right) {
124+
* this.val = val;
125+
* this.left = left;
126+
* this.right = right;
127+
* }
128+
* }
129+
*/
130+
class Solution {
131+
public List<List<String>> printTree(TreeNode root) {
132+
int h = height(root);
133+
int m = h + 1, n = (1 << (h + 1)) - 1;
134+
String[][] res = new String[m][n];
135+
for (int i = 0; i < m; ++i) {
136+
Arrays.fill(res[i], "");
137+
}
138+
dfs(root, res, h, 0, (n - 1) / 2);
139+
List<List<String>> ans = new ArrayList<>();
140+
for (String[] t : res) {
141+
ans.add(Arrays.asList(t));
142+
}
143+
return ans;
144+
}
145+
146+
private void dfs(TreeNode root, String[][] res, int h, int r, int c) {
147+
if (root == null) {
148+
return;
149+
}
150+
res[r][c] = String.valueOf(root.val);
151+
dfs(root.left, res, h, r + 1, c - (1 << (h - r - 1)));
152+
dfs(root.right, res, h, r + 1, c + (1 << (h - r - 1)));
153+
}
154+
155+
private int height(TreeNode root) {
156+
if (root == null) {
157+
return -1;
158+
}
159+
return 1 + Math.max(height(root.left), height(root.right));
160+
}
161+
}
162+
```
163+
164+
### **C++**
165+
166+
```cpp
167+
/**
168+
* Definition for a binary tree node.
169+
* struct TreeNode {
170+
* int val;
171+
* TreeNode *left;
172+
* TreeNode *right;
173+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
174+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
175+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
176+
* };
177+
*/
178+
class Solution {
179+
public:
180+
vector<vector<string>> printTree(TreeNode* root) {
181+
int h = height(root);
182+
int m = h + 1, n = (1 << (h + 1)) - 1;
183+
vector<vector<string>> ans(m, vector<string>(n, ""));
184+
dfs(root, ans, h, 0, (n - 1) / 2);
185+
return ans;
186+
}
187+
188+
void dfs(TreeNode* root, vector<vector<string>>& ans, int h, int r, int c) {
189+
if (!root) return;
190+
ans[r][c] = to_string(root->val);
191+
dfs(root->left, ans, h, r + 1, c - pow(2, h - r - 1));
192+
dfs(root->right, ans, h, r + 1, c + pow(2, h - r - 1));
193+
}
194+
195+
int height(TreeNode* root) {
196+
if (!root) return -1;
197+
return 1 + max(height(root->left), height(root->right));
198+
}
199+
};
200+
```
201+
202+
### **Go**
203+
204+
```go
205+
/**
206+
* Definition for a binary tree node.
207+
* type TreeNode struct {
208+
* Val int
209+
* Left *TreeNode
210+
* Right *TreeNode
211+
* }
212+
*/
213+
func printTree(root *TreeNode) [][]string {
214+
var height func(root *TreeNode) int
215+
height = func(root *TreeNode) int {
216+
if root == nil {
217+
return -1
218+
}
219+
return 1 + max(height(root.Left), height(root.Right))
220+
}
221+
h := height(root)
222+
m, n := h+1, (1<<(h+1))-1
223+
ans := make([][]string, m)
224+
for i := range ans {
225+
ans[i] = make([]string, n)
226+
for j := range ans[i] {
227+
ans[i][j] = ""
228+
}
229+
}
230+
var dfs func(root *TreeNode, r, c int)
231+
dfs = func(root *TreeNode, r, c int) {
232+
if root == nil {
233+
return
234+
}
235+
ans[r][c] = strconv.Itoa(root.Val)
236+
dfs(root.Left, r+1, c-int(math.Pow(float64(2), float64(h-r-1))))
237+
dfs(root.Right, r+1, c+int(math.Pow(float64(2), float64(h-r-1))))
238+
}
239+
240+
dfs(root, 0, (n-1)/2)
241+
return ans
242+
}
85243
244+
func max(a, b int) int {
245+
if a > b {
246+
return a
247+
}
248+
return b
249+
}
86250
```
87251

88252
### **...**

0 commit comments

Comments
 (0)