-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathLCA.cpp
69 lines (58 loc) · 1.72 KB
/
LCA.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
#include<bits/stdc++.h>
using namespace std;
class TreeNode
{
public:
int val;
TreeNode *left, *right;
};
// Assuming the given 2 nodes always exist
TreeNode* LCA_binaryTree(TreeNode* root, int n1, int n2) {
if(!root)
return NULL;
if(root->val == n1 || root->val == n2)
return root;
TreeNode* left_lca = LCA_binaryTree(root->left,n1,n2);
TreeNode* right_lca = LCA_binaryTree(root->right,n1,n2);
// if both left and right gives a ptr then root must be LCA
if(left_lca and right_lca)
return root;
// Left must give LCA
if(left_lca)
return left_lca;
else
return right_lca;
}
// For special case of BST
TreeNode* LCA_bst(TreeNode* root, int n1, int n2) {
if(!root)
return NULL;
if(n1 < root->val and n2 < root->val)
return LCA_bst(root->left,n1,n2);
if(n1 > root->val and n2 > root->val)
return LCA_bst(root->right,n1,n2);
// n1 < root->val and n2 > root->val
return root;
}
// -------------------------------- Helpers ---------------------
TreeNode* newNode(int val)
{
TreeNode* Node = new TreeNode();
Node->val = val;
Node->left = Node->right = NULL;
return(Node);
}
int main(){
TreeNode *root = newNode(20);
root->left = newNode(8);
root->right = newNode(22);
root->left->left = newNode(4);
root->left->right = newNode(12);
root->left->right->left = newNode(10);
root->left->right->right = newNode(14);
int n1 = 10, n2 = 14;
TreeNode *t = LCA_binaryTree(root, n1, n2);
cout << "LCA of " << n1 << " and " << n2 << " is " << t->val<<endl;
t = LCA_bst(root, n1, n2);
cout << "LCA of " << n1 << " and " << n2 << " is " << t->val<<endl;
}