-
Notifications
You must be signed in to change notification settings - Fork 0
/
102_BinaryTreeLevelOrderTraversal102.java
156 lines (129 loc) · 3.9 KB
/
102_BinaryTreeLevelOrderTraversal102.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Given a binary tree, return the level order traversal of its nodes' values.
* (ie, from left to right, level by level).
*
* For example:
* Given binary tree [3,9,20,null,null,15,7],
* 3
* / \
* 9 20
* / \
* 15 7
* return its level order traversal as:
* [
* [3],
* [9,20],
* [15,7]
* ]
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.LinkedList;
import java.util.Queue;
import java.util.ArrayList;
import java.util.List;
public class BinaryTreeLevelOrderTraversal102 {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
List<List<Integer>> result = new ArrayList<>();
int level = 0;
if (root == null) {
return result;
} else {
q.add(root);
}
helper(q, result, 0);
return result;
}
private void helper(Queue<TreeNode> q, List<List<Integer>> result, int level) {
int s = q.size();
if (s == 0) {
return;
}
addLevel(result, level);
int i = 0;
while (i < s) {
TreeNode now = q.poll();
result.get(level).add(now.val);
if (now.left != null) {
q.add(now.left);
}
if (now.right != null) {
q.add(now.right);
}
i++;
}
helper(q, result, level + 1);
}
private void addLevel(List<List<Integer>> result, int level) {
while (result.size() <= level) {
result.add(new ArrayList<Integer>());
}
}
/**
* https://discuss.leetcode.com/topic/7647/java-solution-with-a-queue-used
*/
public List<List<Integer>> levelOrder2(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
List<List<Integer>> wrapList = new LinkedList<List<Integer>>();
if(root == null) return wrapList;
queue.offer(root);
while(!queue.isEmpty()){
int levelNum = queue.size();
List<Integer> subList = new LinkedList<Integer>();
for(int i=0; i<levelNum; i++) {
if(queue.peek().left != null) queue.offer(queue.peek().left);
if(queue.peek().right != null) queue.offer(queue.peek().right);
subList.add(queue.poll().val);
}
wrapList.add(subList);
}
return wrapList;
}
public List<List<Integer>> levelOrder3(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
List<Integer> level = new ArrayList<>();
int cnt = queue.size();
for (int i = 0; i < cnt; i++) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
res.add(level);
}
return res;
}
/**
* https://discuss.leetcode.com/topic/7332/java-solution-using-dfs
*/
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
levelHelper(res, root, 0);
return res;
}
public void levelHelper(List<List<Integer>> res, TreeNode root, int height) {
if (root == null) return;
if (height >= res.size()) {
res.add(new LinkedList<Integer>());
}
res.get(height).add(root.val);
levelHelper(res, root.left, height+1);
levelHelper(res, root.right, height+1);
}
}