Skip to content

Commit

Permalink
Lowest common ancestor of a binary search tree (#249)
Browse files Browse the repository at this point in the history
* add lowest common ancestor bst

* impl lowest common ancestor bst
  • Loading branch information
SKTT1Ryze authored Apr 25, 2024
1 parent 0e8c80f commit 466515c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
77 changes: 77 additions & 0 deletions leetcode-cc/LowestCommonAncestorBST.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(
PLowestCommonAncestorBST, 235, DIFFI_MEDIUM, TOPIC_ALGORITHMS,
"Lowest Common Ancestor of a Binary Search Tree",
"Given a binary search tree (BST), find the lowest common ancestor (LCA) "
"node of two given nodes in the BST.",
{"Binary Tree"});

class SLowestCommonAncestorBST : public ISolution {
public:
size_t problemId() const override { return 235; }
string name() const override {
return ("Solution for " + string("Lowest Common Ancestor BST"));
}
string location() const override { return __FILE_NAME__; }
int test() const override { return 0; };
int benchmark() const override { return 0; }

private:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p,
TreeNode* q) const {
TreeNode* left = nullptr;
TreeNode* right = nullptr;
TreeNode* target = root;

if (p->val > q->val) {
left = q;
right = p;
} else {
left = p;
right = q;
}

while (!(target->val >= left->val && target->val <= right->val)) {
if (target->val < left->val) target = target->right;
if (target->val > right->val) target = target->left;
}

if (target->val == left->val || target->val == right->val) return target;

while (true) {
if (this->isCommonAncestor(target->left, left, true) &&
this->isCommonAncestor(target->left, right, false)) {
target = target->left;
} else if (this->isCommonAncestor(target->right, left, true) &&
this->isCommonAncestor(target->right, right, false)) {
target = target->right;
} else {
break;
}
}

return target;
}

bool isCommonAncestor(TreeNode* root, TreeNode* node, bool isLeft) const {
if (root == nullptr) return false;
if (isLeft) {
if (root->val == node->val) {
return true;
} else {
return this->isCommonAncestor(root->left, node, isLeft);
}
} else {
if (root->val == node->val) {
return true;
} else {
return this->isCommonAncestor(root->right, node, isLeft);
}
}
}
};
8 changes: 8 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "../leetcode-cc/LongestCommonPrefix.hpp"
#include "../leetcode-cc/LongestCommonSubsequence.hpp"
#include "../leetcode-cc/LongestConsecutiveSeq.hpp"
#include "../leetcode-cc/LowestCommonAncestorBST.hpp"
#include "../leetcode-cc/LruCache.hpp"
#include "../leetcode-cc/MajorityElement.hpp"
#include "../leetcode-cc/MaxDepthOfBTree.hpp"
Expand Down Expand Up @@ -944,5 +945,12 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SZeroOneMatrix>(); });

handle->registerProblem([]() -> ArcProblem {
return std::make_shared<PLowestCommonAncestorBST>();
});
handle->registerSolution([]() -> ArcSolution {
return std::make_shared<SLowestCommonAncestorBST>();
});

return 0;
}

0 comments on commit 466515c

Please sign in to comment.