Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions lowest_common_ancestor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <iostream>

struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root) return nullptr;

if (p->val < root->val && q->val < root->val) {
return lowestCommonAncestor(root->left, p, q);
} else if (p->val > root->val && q->val > root->val) {
return lowestCommonAncestor(root->right, p, q);
} else {
return root;
}
}

int main() {
// Create a BST
TreeNode* root = new TreeNode(20);
root->left = new TreeNode(10);
root->right = new TreeNode(30);
root->left->left = new TreeNode(5);
root->left->right = new TreeNode(15);
root->right->left = new TreeNode(25);
root->right->right = new TreeNode(35);

TreeNode* p = root->left->left; // Node with value 5
TreeNode* q = root->left->right; // Node with value 15

TreeNode* lca = lowestCommonAncestor(root, p, q);

if (lca) {
std::cout << "Lowest Common Ancestor: " << lca->val << std::endl;
} else {
std::cout << "No common ancestor found." << std::endl;
}

// Clean up memory (not shown for brevity)
return 0;
}