-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeLevelOrderTraversal.java
More file actions
42 lines (40 loc) · 1.27 KB
/
Copy pathBinaryTreeLevelOrderTraversal.java
File metadata and controls
42 lines (40 loc) · 1.27 KB
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
//time: O(n)
//space: o(n)
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
List<Integer> list = new ArrayList<>();
int size = queue.size();
for (int i = 0; i<size; i++){
TreeNode cur = queue.poll();
System.out.println(cur.val);
if(cur.left!=null) queue.offer(cur.left);
if(cur.right!= null) queue.offer(cur.right);
list.add(cur.val);
}
res.add(list);
}
return res;
}
}
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null) return res;
helper(root, 0, res);
return res;
}
private void helper(TreeNode root, int level, List<List<Integer>>res){
if(root == null) return;
if(res.size() <= level){
res.add(new ArrayList<>());
}
res.get(level).add(root.val);
helper(root.left, level+1, res);
helper(root.right,level+1,res);
}
}