vnvaditya
BAN USER
Comments (6)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
int getHeight(Node p) {
int height = 0;
while (p) {
height++;
p = p->parent;
}
return height;
}
Node Ancestor(Node p, Node q) {
int h1 = getHeight(p);
int h2 = getHeight(q);
if (h1 > h2) {
swap(h1, h2);
}
for (int h = 0; h < |h2-h1|; h++)
q = q->parent;
while (p && q) {
if (p == q) return p;
p = p->parent;
q = q->parent;
}
return null;
}
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
What if an existing user logins in again ??
- vnvaditya February 24, 2013Search will be O(n) in that case.
LinkedHashMap is the correct solution