-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-LCAInABST.cpp
83 lines (82 loc) · 2.2 KB
/
06-LCAInABST.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
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <set>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
struct Node {
public:
int val;
Node* left;
Node* right;
Node* parent;
};
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==NULL || root==p || root==q)
return root;
TreeNode* left=lowestCommonAncestor(root->left,p,q);
TreeNode* right=lowestCommonAncestor(root->right,p,q);
if(left==NULL)
return right;
else if(right==NULL)
return left;
else
return root;
}
TreeNode* lcaOptimal(TreeNode* root,TreeNode* p,TreeNode* q)
{
if(root==NULL)
return root;
int mini=min(p->val,q->val);
int maxi=max(p->val,q->val);
while(root!=NULL)
{
if(root->val>=mini && root->val<=maxi)
return root;
else if(root->val<=mini)
root=root->right;
else if(root->val>=maxi)
root=root->left;
}
return NULL;
}
// Lowest Common Ancestor Part - iii)
// https://leetcode.ca/2020-06-06-1650-Lowest-Common-Ancestor-of-a-Binary-Tree-III/
// 1st method is to use a set -> where the lca will be the first common node traversing from p to q
Node* lcapart3(Node* p,Node* q)
{
set<Node*> s;
Node* temp=p;
while(temp)
{
s.insert(temp);
temp=temp->parent;
}
temp=q;
while(temp)
{
if(s.find(temp)!=s.end())
{
break;
}
temp=temp->parent;
}
return temp;
}
Node* lcapart3Approach2(Node* p,Node* q)
{
Node* a = p, *b = q;
while (a != b) {
a = a->parent == NULL ? q : a->parent;
b = b->parent == NULL ? p : b->parent;
}
return a;
}
};