-
Notifications
You must be signed in to change notification settings - Fork 8
/
leetcode-lowest-common-ancestor
41 lines (40 loc) · 1.09 KB
/
leetcode-lowest-common-ancestor
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
//https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
/**
* 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:
bool findPath(TreeNode* t,vector<TreeNode*>&path, TreeNode* x){
if(t== NULL){
return false;
}
path.push_back(t);
if(t==x){
return true;
}
if(findPath(t->left,path,x) || findPath(t->right,path,x)){
return true;
}
path.pop_back();
return false;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
vector<TreeNode*> path1,path2;
findPath(root,path1,p);
findPath(root,path2,q);
int i=0;
cout<<path1.size()<<path2.size();
while(i<path1.size() && i< path2.size() && path1[i]==path2[i]){
i++;
}
i--;
cout<<i<<endl;
return path1[i];
}
};