/
Solution.java
executable file
·80 lines (65 loc) · 2.24 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
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
75
76
77
78
79
80
import java.util.*;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<Integer> tmpres = new ArrayList<Integer>();
List<List<Integer>> res = new ArrayList<List<Integer>>();
Deque<TreeNode> queue = new LinkedList<TreeNode>(),
next = new LinkedList<TreeNode>();
if(root != null) queue.offer(root);
while(!queue.isEmpty()){
tmpres.clear();
next.clear();
while(!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (cur.left != null) next.offer(cur.left);
if (cur.right != null) next.offer(cur.right);
tmpres.add(cur.val);
}
res.add(new LinkedList<Integer>(tmpres));
queue = new LinkedList<TreeNode>(next);
}
return res;
}
}
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
ArrayList<List<Integer>> rt = new ArrayList<List<Integer>>();
if(root == null) return rt;
final TreeNode END = new TreeNode(0);
Deque<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
queue.add(END);
ArrayList<Integer> level = new ArrayList<Integer>();
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(node == END){
rt.add(new ArrayList<Integer>(level)); // copy
level.clear();
if(!queue.isEmpty()) queue.add(END);
}else{
level.add(node.val);
if(node.left != null) queue.addLast(node.left);
if(node.right != null) queue.addLast(node.right);
}
}
return rt;
}
}