forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
235.c
97 lines (75 loc) · 1.79 KB
/
235.c
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
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
if (root == NULL) return NULL;
if (p == NULL) return q;
if (q == NULL) return p;
struct TreeNode *i, *j, *lca;
i = j = lca = root;
while (i || j) {
if (i->val > p->val) {
i = i->left;
}
else if (i->val < p->val) {
i = i->right;
}
else {
lca = i;
break;
}
if (j->val > q->val) {
j = j->left;
}
else if (j->val < q->val) {
j = j->right;
}
else {
lca = j;
break;
}
if (i->val != j->val) {
break;
}
lca = i;
}
return lca;
}
int main() {
struct TreeNode *r = (struct TreeNode *)calloc(9, sizeof(struct TreeNode));
struct TreeNode *t = r;
struct TreeNode *p = NULL, *q = NULL;
t->val = 6;
t->left = r + 1;
t->right = r + 2;
t = t->left;
t->val = 2;
t->left = r + 3;
t->right = r + 4;
t->left->val = 0;
t = r + 4;
t->val = 4;
t->left = r + 5;
t->right = r + 6;
t->left->val = 3;
t->right->val = 5;
t = r->right;
t->val = 8;
t->left = r + 7;
t->right = r + 8;
t->left->val = 7;
t->right->val = 9;
p = r + 1; /* 2 */
q = r + 2; /* 8 */
struct TreeNode *lca = lowestCommonAncestor(r, p, q);
printf("lca of %d and %d is: %d\n", p->val, q->val, lca->val);
p = r + 1; /* 2 */
q = r + 4; /* 4 */
lca = lowestCommonAncestor(r, p, q);
printf("lca of %d and %d is: %d\n", p->val, q->val, lca->val);
return 0;
}