-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBSTLevelOrderTraversal_II.java
38 lines (38 loc) · 1.29 KB
/
BSTLevelOrderTraversal_II.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
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (root == null) return result;
Stack<ArrayList<Integer>> aStack = new Stack<ArrayList<Integer>>();
Queue<TreeNode> nodeQ = new LinkedList<TreeNode>();
TreeNode dummy = new TreeNode(0);
nodeQ.add(root);
nodeQ.add(dummy);
while(nodeQ.size() > 0){
ArrayList<Integer> level = new ArrayList<Integer>();
while(nodeQ.peek() != dummy){
TreeNode temp = nodeQ.poll();
level.add(temp.val);
if (temp.left != null) nodeQ.add(temp.left);
if (temp.right != null) nodeQ.add(temp.right);
}
aStack.add(level);
if (nodeQ.size() == 1 && nodeQ.peek() == dummy) break;
nodeQ.add(nodeQ.poll());
}
while(!aStack.empty()){
result.add(aStack.pop());
}
return result;
}
}