Repository navigation
Expand file tree
/
Copy pathLevelOrder.java
More file actions
48 lines (45 loc) · 1.46 KB
/
Copy pathLevelOrder.java
File metadata and controls
48 lines (45 loc) · 1.46 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
42
43
44
45
46
47
48
/*Copyright (c) Dec 21, 2014 CareerMonk Publications and others.
* E-Mail : info@careermonk.com
* Creation Date : 2015-01-10 06:15:46
* Last modification : 2006-05-31
by : Narasimha Karumanchi
* Book Title : Data Structures And Algorithms Made In Java
* Warranty : This software is provided "as is" without any
* warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose.
*
*/
package chapter06trees;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class LevelOrder {
public ArrayList<ArrayList<Integer>> levelOrder(BinaryTreeNode root) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if (root == null)
return res;
// Initialization
Queue<BinaryTreeNode> q = new LinkedList<BinaryTreeNode>();
q.offer(root);
q.offer(null);
ArrayList<Integer> curr = new ArrayList<Integer>();
while (!q.isEmpty()) {
BinaryTreeNode tmp = q.poll();
if (tmp != null) {
curr.add(tmp.data);
if (tmp.getLeft() != null)
q.offer(tmp.getLeft());
if (tmp.right != null)
q.offer(tmp.right);
} else {
ArrayList<Integer> c_curr = new ArrayList<Integer>(curr);
res.add(c_curr);
curr.clear(); // Java will clear the reference, so have to new an new ArrayList.
// completion of a level;
if (!q.isEmpty())
q.offer(null);
}
}
return res;
}
}