-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowest-common_ancestor_in_bianry_tree.c++
66 lines (65 loc) · 1.68 KB
/
lowest-common_ancestor_in_bianry_tree.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
/**
* 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) {
queue<TreeNode*>q1;
q1.push(root);
unordered_map<TreeNode*,TreeNode*>mp;
mp[root]=NULL;
while(!q1.empty()){
int n=q1.size();
for(int i=0;i<n;i++){
TreeNode*t=q1.front();
q1.pop();
if(t->left){
mp[t->left]=t;
q1.push(t->left);
}
if(t->right){
q1.push(t->right);
mp[t->right]=t;
}
}
}
vector<TreeNode*>arr1;
vector<TreeNode*>arr2;
TreeNode*t1=p;
TreeNode*t2=q;
while(t1!=NULL){
arr1.push_back(t1);
t1=mp[t1];
}
while(t2!=NULL){
arr2.push_back(t2);
t2=mp[t2];
}
unordered_map<TreeNode*,bool>b1;
unordered_map<TreeNode*,bool>b2;
for(int i=0;i<arr1.size();i++){
b1[arr1[i]]=true;
}
for(int i=0;i<arr2.size();i++){
b2[arr2[i]]=true;
}
if(b1[q]==true){
return q;
}
if(b2[p]==true){
return p;
}
for(int i=0;i<arr1.size();i++){
if(b2[arr1[i]]==true){
return arr1[i];
}
}
return NULL;
}
};