-
-
Notifications
You must be signed in to change notification settings - Fork 611
/
BinaryTreeLevelOrderTraversal.java
84 lines (72 loc) · 2.28 KB
/
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
75
76
77
78
79
80
81
82
83
84
package problems.easy;
import problems.utils.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;
/**
* Created by sherxon on 2016-12-24.
*/
public class BinaryTreeLevelOrderTraversal {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if (root == null) return list;
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
List<Integer> l = new ArrayList<>();
l.add(root.val);
list.add(l);
int level = 1;
while (!q.isEmpty()) {
TreeNode x = q.remove();
level--;
if (x.left != null) q.add(x.left);
if (x.right != null) q.add(x.right);
if (level == 0 && !q.isEmpty()) {
l = new ArrayList<>();
for (TreeNode tr : q) {
level++;
l.add(tr.val);
}
list.add(l);
}
}
return list;
}
public List<List<Integer>> levelOrder3(TreeNode root) {
List<List<Integer>> l = new LinkedList<>();
if (root == null) return l;
Queue<TreeNode> level = new LinkedList<>();
Queue<TreeNode> qq = new LinkedList<>();
qq.add(root);
level.add(root);
List<Integer> temp = new ArrayList<>();
temp.add(root.val);
l.add(temp);
while (!qq.isEmpty()) {
TreeNode t = qq.remove();
level.remove();
if (t.left != null) qq.add(t.left);
if (t.right != null) qq.add(t.right);
if (level.isEmpty() && !qq.isEmpty()) {
List<Integer> lev = qq.stream().map(treeNode -> treeNode.val).collect(Collectors.toList());
l.add(lev);
level.addAll(new LinkedList<>(qq));
}
}
return l;
}
void levelOrder2(TreeNode root) {
if (root == null) return;
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
TreeNode x = q.poll();
if (x.left != null)
q.add(x.left);
if (x.right != null)
q.add(x.right);
}
}
}