forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
43 lines (33 loc) · 1.21 KB
/
Solution.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
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import javafx.util.Pair;
/// BFS
/// Time Complexity: O(n), where n is the number of nodes in the tree
/// Space Complexity: O(n)
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
ArrayList<List<Integer>> res = new ArrayList<>();
if(root == null)
return res;
LinkedList<Pair<TreeNode, Integer>> queue = new LinkedList<>();
queue.addLast(new Pair<>(root, 0));
while(!queue.isEmpty()){
Pair<TreeNode, Integer> front = queue.removeFirst();
TreeNode node = front.getKey();
int level = front.getValue();
if(level == res.size())
res.add(new ArrayList<>());
assert level < res.size();
res.get(level).add(node.val);
if(node.left != null)
queue.addLast(new Pair<>(node.left, level + 1));
if(node.right != null)
queue.addLast(new Pair<>(node.right, level + 1));
}
return res;
}
}