-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCAOfBinarySearchTree.cpp
95 lines (90 loc) · 2.45 KB
/
LCAOfBinarySearchTree.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
84
85
86
87
88
89
90
91
92
93
94
95
// Implemented by Kritagya Kumra
#include <iostream>
#include <vector>
using namespace std;
// Following is the Binary Tree node structure
class BinaryTreeNode
{
public:
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
BinaryTreeNode(int data)
{
this->data = data;
left = NULL;
right = NULL;
}
};
BinaryTreeNode *LCAinaBST(BinaryTreeNode *root, BinaryTreeNode *P, BinaryTreeNode *Q)
{
// if (root == NULL)
// return NULL;
// if (root->data < P->data || root->data < Q->data)
// {
// // Move to the right side
// return LCAinaBST(root->right, P, Q);
// }
// else if (root->data > P->data || root->data > Q->data)
// {
// // Move to the left side
// return LCAinaBST(root->left, P, Q);
// }
// else
// {
// // Move to the root
// return root;
// }
if (root == NULL)
return NULL;
while (root != NULL)
{
if (root->data < P->data && root->data < Q->data)
{
// Move to the right side
root = root->right;
}
else if (root->data > P->data && root->data > Q->data)
{
// Move to the left side
root = root->left;
}
else
{
// Move to the root
return root;
}
}
}
int main()
{
// node *root = NULL;
// Creation of the tree
// root = buildTree(root);
// cout << "Level Order Traversal " << endl;
// levelOrderTraversal(root);
// cout << "Reverse Level Order Traversal " << endl;
// reverseLevelOrderTraversal(root);
// cout << "Inorder Traversal " << endl;
// inorderTraversal(root);
// cout << endl;
// cout << "Preorder Traversal " << endl;
// preorderTraversal(root);
// cout << endl;
// cout << "Postorder Traversal " << endl;
// postorderTraversal(root);
// cout << endl;
// 1 3 7 -1 -1 11 -1 -1 5 17 -1 -1 -1
// buildFromLevelOrder(root);
// levelOrderTraversal(root);
BinaryTreeNode *root = new BinaryTreeNode(10);
root->left = new BinaryTreeNode(8);
root->right = new BinaryTreeNode(20);
root->left->left = new BinaryTreeNode(3);
root->left->right = new BinaryTreeNode(9);
root->right->left = new BinaryTreeNode(12);
cout << "LCA of both th numbers is " << endl;
cout << LCAinaBST(root, root->right->left, root->left->left)->data;
return 0;
}
// Implemented by Kritagya Kumra