-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP0102_BinaryTreeLevelOrderTraversal.java
74 lines (69 loc) · 2.2 KB
/
P0102_BinaryTreeLevelOrderTraversal.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package yyl.leetcode.p01;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import yyl.leetcode.bean.TreeNode;
import yyl.leetcode.util.Assert;
/**
* <h3>二叉树的层序遍历</h3><br>
* 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。<br>
*
* <pre>
* 示例:
* 二叉树:[3,9,20,null,null,15,7],
*
* 3
* / \
* 9 20
* / \
* 15 7
*
* 返回其层次遍历结果:
*
* [
* [3],
* [9,20],
* [15,7]
* ]
* </pre>
*/
public class P0102_BinaryTreeLevelOrderTraversal {
public static void main(String[] args) {
Solution solution = new Solution();
TreeNode root = TreeNode.create("[3,9,20,null,null,15,7]");
List<List<Integer>> expected = Arrays.asList(Arrays.asList(3), Arrays.asList(9, 20), Arrays.asList(15, 7));
List<List<Integer>> actual = solution.levelOrder(root);
Assert.assertEquals(expected, actual);
}
// 广度优先搜索
// 时间复杂度:O(n),每个点进队出队各一次, 故渐进时间复杂度为 O(n)。
// 空间复杂度:O(n),队列中元素的个数不超过 n 个,故渐进空间复杂度为 O(n)。
static class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> answer = new ArrayList<>();
if (root == null) {
return answer;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> values = new ArrayList<>();
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
values.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
answer.add(values);
}
return answer;
}
}
}