Skip to content

Commit 43bc9ec

Browse files
committed
Add solution 102
1 parent e5b79a8 commit 43bc9ec

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Complete solutions to Leetcode problems, updated daily.
5858
| 086 | [Partition List](https://github.com/doocs/leetcode/tree/master/solution/086.Partition%20List) | `Linked List`, `Two Pointers` |
5959
| 092 | [Reverse Linked List II](https://github.com/doocs/leetcode/tree/master/solution/092.Reverse%20Linked%20List%20II) | `Linked List` |
6060
| 094 | [Binary Tree Inorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/094.Binary%20Tree%20Inorder%20Traversal) | `Hash Table`, `Stack`, `Tree` |
61+
| 102 | [Binary Tree Level Order Traversal](https://github.com/doocs/leetcode/tree/master/solution/102.Binary%20Tree%20Level%20Order%20Traversal) | `Tree`, `Breadth-first Search` |
6162
| 127 | [Word Ladder](https://github.com/doocs/leetcode/tree/master/solution/127.Word%20Ladder) | `Breadth-first Search` |
6263
| 130 | [Surrounded Regions](https://github.com/doocs/leetcode/tree/master/solution/130.Surrounded%20Regions) | `Depth-first Search`, `Breadth-first Search`, `Union Find` |
6364
| 144 | [Binary Tree Preorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## 二叉树的层次遍历
2+
### 题目描述
3+
4+
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
5+
6+
例如:
7+
给定二叉树: `[3,9,20,null,null,15,7]`,
8+
```
9+
10+
3
11+
/ \
12+
9 20
13+
/ \
14+
15 7
15+
```
16+
17+
返回其层次遍历结果:
18+
```
19+
[
20+
[3],
21+
[9,20],
22+
[15,7]
23+
]
24+
```
25+
26+
### 解法
27+
利用队列,存储二叉树结点。`size` 表示每一层的结点数量,也就是内层循环的次数。
28+
29+
```java
30+
/**
31+
* Definition for a binary tree node.
32+
* public class TreeNode {
33+
* int val;
34+
* TreeNode left;
35+
* TreeNode right;
36+
* TreeNode(int x) { val = x; }
37+
* }
38+
*/
39+
class Solution {
40+
public List<List<Integer>> levelOrder(TreeNode root) {
41+
List<List<Integer>> res = new ArrayList<>();
42+
if (root == null) {
43+
return res;
44+
}
45+
46+
Queue<TreeNode> queue = new LinkedList<>();
47+
queue.offer(root);
48+
while (!queue.isEmpty()) {
49+
int size = queue.size();
50+
List<Integer> list = new ArrayList<>();
51+
while ((size--) > 0) {
52+
TreeNode node = queue.poll();
53+
list.add(node.val);
54+
if (node.left != null) {
55+
queue.offer(node.left);
56+
}
57+
if (node.right != null) {
58+
queue.offer(node.right);
59+
}
60+
}
61+
res.add(list);
62+
}
63+
64+
return res;
65+
}
66+
}
67+
68+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public List<List<Integer>> levelOrder(TreeNode root) {
3+
List<List<Integer>> res = new ArrayList<>();
4+
if (root == null) {
5+
return res;
6+
}
7+
8+
Queue<TreeNode> queue = new LinkedList<>();
9+
queue.offer(root);
10+
while (!queue.isEmpty()) {
11+
int size = queue.size();
12+
List<Integer> list = new ArrayList<>();
13+
while ((size--) > 0) {
14+
TreeNode node = queue.poll();
15+
list.add(node.val);
16+
if (node.left != null) {
17+
queue.offer(node.left);
18+
}
19+
if (node.right != null) {
20+
queue.offer(node.right);
21+
}
22+
}
23+
res.add(list);
24+
}
25+
26+
return res;
27+
}
28+
}

0 commit comments

Comments
 (0)