-
Notifications
You must be signed in to change notification settings - Fork 22
/
FindLCAInTreeForestUsingLevels
179 lines (131 loc) · 3.73 KB
/
FindLCAInTreeForestUsingLevels
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
172
173
174
175
176
177
178
179
/*
*
Question:
The closest common ancestor in a tree forest in O(1) memory.
class Node {
Node* parent; // == null for root of tree
Node* left;
Node* right;
}
Node* tree_forest[]; // array of pointers which points to roots of each tree respectively
Node* closest_common_ancestor(Node* n1, Node* n2) {
// your solution
}
Example:
| a | j
| / \ | /
| b c | h
| / / \ |
|d e f |
for e and d CCA is a
for e and f CCA is c
for e and c CCA is c
for h and d CCA is null
CONSTRAINS: O(1) ADDITIONAL MEMORY
Source: http://www.careercup.com/question?id=6715482117767168
* ALGORITHM AND HINT:
Interviewer gave me a hint: it's better to consider the case when n1 and n2 lays on the same level of tree
(in my example b, c, h lays on same level of trees)
Case 1.
Let's assume that n1 and n2 lays on the same level.
In this case we can just go up over the tree simultaneously.
Then we face situation when n1 == n2 we done.
Case 2.
1) We need to count level for both nodes.
2) Align levels by following parent on node with higher level.
3) Do Case 1 solution.
* Algorithm:
1. GET LEVELS of the two nodes
2. Make the LEVELS EQUAL irrespective of the fact whether nodes are in SAME tree or DIFFERENT tree
3. TRAVERSE THE PARENTS unless both parents are equal OR either parent is null
*/
package FindLCAInTreeForest;
public class UsingNodeLevels {
public static void main(String[] args) {
Node a = new Node('a');
Node b = new Node('b');
Node c = new Node('c');
Node d = new Node('d');
Node e = new Node('e');
Node f = new Node('f');
Node j = new Node('j');
Node h = new Node('h');
// Create the first tree BOTTOM UP fashion
// Part 1: Set Parent
e.parent=c;
f.parent=c;
d.parent=b;
b.parent=a;
c.parent=a;
// Part 2: Set Left and Right Child
a.left=b;
a.right=c;
b.left=d;
c.left=e;
c.right=f;
// Create the second tree BOTTOM UP fashion
// Part 1: Set Parent
h.parent=j;
// Part 2: Set Left and Right Child
j.left=h;
// Create a forest
Node[] treeForest = new Node[2]; // array of pointers which points to roots of each tree respectively
treeForest[0]=a;
treeForest[1]=j;
System.out.println("The LCA of e and h which are in DIFFERENT LEVEL DIFFERENT TREES is: "+getLCA(e, h));
System.out.println("The LCA of c and h which are in SAME LEVEL DIFFERENT TREES is: "+getLCA(c, h));
System.out.println("The LCA of d and c which are in DIFFERENT LEVEL SAME TREES is: "+getLCA(d, c));
System.out.println("The LCA of f and e which are in SAME LEVEL SAME TREES is: "+getLCA(f, e));
}
public static Character getLCA(Node a, Node b){
int level1 = getLevel(a);
int level2 = getLevel(b);
// make the two levels equal
while(level1>level2){
a=a.parent;
level1--;
}
while(level2>level1){
b=b.parent;
level2--;
}
// Now both the levels are same
/*
* Since both the levels are now same, hence traverse the parents of the node until the parents are EQUAL
*/
while(a!=b && a!=null && b!=null){
a=a.parent;
b=b.parent;
}
// if a and b belong to different trees
if(a==null || b==null)
return null;
// if a and b belong to same trees then either return a.data OR b.data
return a.data; // OR return b.data;
}
public static int getLevel(Node n){
int level = 0;
while(n!=null){
n=n.parent;
level++;
}
return level;
}
}
class Node {
Node parent; // == null for root of tree
Node left;
Node right;
char data;
public Node(char data){
this.data = data;
this.parent = null;
this.left = null;
this.right = null;
}
}
/*
Analysis:
Time Complexity = O(max(levelA,levelB))
Space Complexity = O(1)
*/