forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZigzagLevelOrder.java
171 lines (136 loc) · 4.51 KB
/
ZigzagLevelOrder.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package Algorithms;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import Algorithms.tree.TreeNode;
public class ZigzagLevelOrder {
public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
// when dir is true, direction is from left to right;
boolean dir = true;
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
if (root == null) {
return rst;
}
Queue<TreeNode> q = new LinkedList<TreeNode>();
Stack<Integer> s = new Stack<Integer>();
q.offer(root);
while (!q.isEmpty()) {
ArrayList<Integer> list = new ArrayList<Integer>();
int size = q.size();
for (int i = 0; i < size; i++){
TreeNode curr = q.poll();
if (dir == true) {
list.add(curr.val);
} else {
s.add(curr.val);
}
if (curr.left != null) {
q.offer(curr.left);
}
if (curr.right != null) {
q.offer(curr.right);
}
}
if (dir == true) {
rst.add(list);
dir = false;
} else {
while (!s.isEmpty()) {
list.add(s.pop());
}
rst.add(list);
dir = true;
}
}
return rst;
}
public static TreeNode LCA(TreeNode root,TreeNode a,TreeNode b){
TreeNode left=null,right=null;
if(root==null) return root;
if(root==a || root==b) return root;
left=LCA(root.left,a,b);
right=LCA(root.right,a,b);
if(left!=null && right!=null)return root;
return (left!=null)?left:right;
}
/*
private TreeNode getRoot(TreeNode node) {
while (node.parent != null) {
node = node.parent;
}
return node;
}*/
public static boolean isDuplicate(String s) {
if (s == null) {
return false;
}
int check = 0;
for(int i=0; i<s.length(); i++) {
int val = (int)(s.charAt(i)-'a');
if((check & (1<<val)) == 0) {
check |= (1<<val);
} else{
return false;
}
}
return true;
}
private TreeNode getAncestor(TreeNode root, TreeNode node1, TreeNode node2) {
if (root == null || root == node1 || root == node2) {
return root;
}
// Divide
TreeNode left = getAncestor(root.left, node1, node2);
TreeNode right = getAncestor(root.right, node1, node2);
// Conquer
if (left != null && right != null) {
return root;
}
if (left != null) {
return left;
}
if (right != null) {
return right;
}
return null;
}
/*
public TreeNode latestCommonAncestor(TreeNode node1, TreeNode node2) {
if (node1 == null || node2 == null) {
return null;
}
TreeNode root = getRoot(node1);
return getAncestor(root, node1, node2);
}*/
public static void main(String[] args) {
ZigzagLevelOrder p = new ZigzagLevelOrder();
/**
* 1
* 2 3
* 4 5
*7
*/
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.right.right = new TreeNode(6);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.left.left.left = new TreeNode(7);
//ArrayList<ArrayList<Integer>> rst = p.zigzagLevelOrder(root);
ArrayList<ArrayList<Integer>> rst = p.zigzagLevelOrder(root);
TreeNode rst2 = p.LCA(root, root.left.left.left, root.left.right);
System.out.printf(rst2.toString());
//abca
//97 98
//int[] A = new int[256]; //O(1) space complexity
//String s = null;
//A[97] = true;
//A[98] = true;
//A[99] = true;
//boolean rst = ZigzagLevelOrder.isDuplicate(s);
//System.out.printf("test, %s", rst);
//O(1)
}
}