-
Notifications
You must be signed in to change notification settings - Fork 0
/
0236.cpp
72 lines (63 loc) · 1.6 KB
/
0236.cpp
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
// return func1(root, p, q);
return func2(root, p, q);
}
TreeNode *func1(TreeNode *root, TreeNode *p, TreeNode *q) {
vector<TreeNode *> p_anc;
vector<TreeNode *> q_anc;
findAncestor(root, p, p_anc);
findAncestor(root, q, q_anc);
int p_size = p_anc.size();
int q_size = q_anc.size();
int size = min(p_size, q_size);
TreeNode *ancestor = NULL;
for (int i = 0; i < size; i++) {
if (p_anc[i] == q_anc[i]) {
ancestor = p_anc[i];
} else {
break;
}
}
return ancestor;
}
bool findAncestor(TreeNode *root, TreeNode *node,
vector<TreeNode *> &ancestor) {
if (root == NULL) {
return false;
}
ancestor.emplace_back(root);
if (root == node) {
return true;
}
if (findAncestor(root->left, node, ancestor)) {
return true;
}
if (findAncestor(root->right, node, ancestor)) {
return true;
}
ancestor.pop_back();
return false;
}
TreeNode *func2(TreeNode *root, TreeNode *p, TreeNode *q) {
if (root == NULL || root == p || root == q) {
return root;
}
TreeNode *left = func2(root->left, p, q);
TreeNode *right = func2(root->right, p, q);
if (left != NULL && right != NULL) {
return root;
}
return left != NULL ? left : right;
}
};