Skip to content

Commit a77207c

Browse files
committed
feat: add python and java solutions to lcof problem
添加《剑指 Offer》题解:面试题32 - II. 从上到下打印二叉树 II
1 parent e4ff1e4 commit a77207c

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# [面试题32 - II. 从上到下打印二叉树 II](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/)
2+
3+
## 题目描述
4+
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
5+
6+
**例如:**
7+
8+
给定二叉树: `[3,9,20,null,null,15,7]`,
9+
10+
```
11+
3
12+
/ \
13+
9 20
14+
/ \
15+
15 7
16+
```
17+
18+
返回其层次遍历结果:
19+
20+
```
21+
[
22+
[3],
23+
[9,20],
24+
[15,7]
25+
]
26+
```
27+
28+
**提示:**
29+
30+
- `节点总数 <= 1000`
31+
32+
## 解法
33+
### Python3
34+
```python
35+
# Definition for a binary tree node.
36+
# class TreeNode:
37+
# def __init__(self, x):
38+
# self.val = x
39+
# self.left = None
40+
# self.right = None
41+
42+
from queue import Queue
43+
44+
class Solution:
45+
def levelOrder(self, root: TreeNode) -> List[List[int]]:
46+
if root is None:
47+
return []
48+
q = Queue()
49+
q.put(root)
50+
cnt = 1
51+
res = []
52+
while not q.empty():
53+
t = []
54+
num = 0
55+
for _ in range(cnt):
56+
node = q.get()
57+
t.append(node.val)
58+
if node.left:
59+
q.put(node.left)
60+
num += 1
61+
if node.right:
62+
q.put(node.right)
63+
num += 1
64+
res.append(t)
65+
cnt = num
66+
return res
67+
```
68+
69+
### Java
70+
```java
71+
/**
72+
* Definition for a binary tree node.
73+
* public class TreeNode {
74+
* int val;
75+
* TreeNode left;
76+
* TreeNode right;
77+
* TreeNode(int x) { val = x; }
78+
* }
79+
*/
80+
class Solution {
81+
public List<List<Integer>> levelOrder(TreeNode root) {
82+
if (root == null) {
83+
return new ArrayList<>();
84+
}
85+
Queue<TreeNode> q = new LinkedList<>();
86+
int cnt = 1;
87+
q.offer(root);
88+
List<List<Integer>> res = new ArrayList<>();
89+
while (!q.isEmpty()) {
90+
List<Integer> t = new ArrayList<>();
91+
int num = 0;
92+
while (cnt-- > 0) {
93+
TreeNode node = q.poll();
94+
t.add(node.val);
95+
if (node.left != null) {
96+
q.offer(node.left);
97+
++num;
98+
}
99+
if (node.right != null) {
100+
q.offer(node.right);
101+
++num;
102+
}
103+
}
104+
res.add(t);
105+
cnt = num;
106+
}
107+
return res;
108+
}
109+
}
110+
```
111+
112+
### ...
113+
```
114+
115+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public List<List<Integer>> levelOrder(TreeNode root) {
12+
if (root == null) {
13+
return new ArrayList<>();
14+
}
15+
Queue<TreeNode> q = new LinkedList<>();
16+
int cnt = 1;
17+
q.offer(root);
18+
List<List<Integer>> res = new ArrayList<>();
19+
while (!q.isEmpty()) {
20+
List<Integer> t = new ArrayList<>();
21+
int num = 0;
22+
while (cnt-- > 0) {
23+
TreeNode node = q.poll();
24+
t.add(node.val);
25+
if (node.left != null) {
26+
q.offer(node.left);
27+
++num;
28+
}
29+
if (node.right != null) {
30+
q.offer(node.right);
31+
++num;
32+
}
33+
}
34+
res.add(t);
35+
cnt = num;
36+
}
37+
return res;
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
from queue import Queue
9+
10+
class Solution:
11+
def levelOrder(self, root: TreeNode) -> List[List[int]]:
12+
if root is None:
13+
return []
14+
q = Queue()
15+
q.put(root)
16+
cnt = 1
17+
res = []
18+
while not q.empty():
19+
t = []
20+
num = 0
21+
for _ in range(cnt):
22+
node = q.get()
23+
t.append(node.val)
24+
if node.left:
25+
q.put(node.left)
26+
num += 1
27+
if node.right:
28+
q.put(node.right)
29+
num += 1
30+
res.append(t)
31+
cnt = num
32+
return res

0 commit comments

Comments
 (0)