-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlca-parent-pointer.cpp
More file actions
67 lines (56 loc) · 1.64 KB
/
lca-parent-pointer.cpp
File metadata and controls
67 lines (56 loc) · 1.64 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// lca in tree with parent pointer
// http://leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-ii.html
// basic idea is to locate both nodes and their height h1, h2, say h1-h2 = d
// we can then find the parent when (h1-d) , h2 meets
#include "leetcode.h"
class Solution {
public:
int hp, hq;
void find_height(TreeNodeWithParent *root, TreeNodeWithParent *p, TreeNodeWithParent *q, int height) {
if (root == nullptr) return;
if (hp >= 0 && hq >= 0) return;
if (root == p) {
hp = height;
}
if (root == q) {
hq = height;
}
find_height(root->left, p, q, height+1);
find_height(root->right, p, q, height+1);
}
TreeNodeWithParent *LCA(TreeNodeWithParent *root, TreeNodeWithParent *p, TreeNodeWithParent *q) {
if (!root || !p || !q) return nullptr;
hp = hq = -1;
find_height(root, p, q, 0);
// p or q is not in the tree
if (hp < 0 || hq < 0) return nullptr;
// make sure hp <= hq
if (hp > hq) {
swap(hp, hq);
swap(p, q);
}
// move q to the same height as p
int d = hq - hp;
while (d > 0) {
--d;
q = q->parent;
}
while (q != p) {
p = p->parent;
q = q->parent;
}
return p;
}
};
int main() {
Solution sol;
TreeNode *node = deserialize_tree(string("1 2 4 # # 5 # # 3 # #"));
TreeNodeWithParent *root = TreeNodeWithParent::fromTreeNode(node);
// TreeNodeWithParent *p = root->right;
TreeNodeWithParent *p = root->left->left;
TreeNodeWithParent *q = root->left->right;
// TreeNodeWithParent *q = root->left;
TreeNodeWithParent *lca = sol.LCA(root, p, q);
cout << lca->val << "\n";
return 0;
}