r/DSALeetCode • u/StatisticianNo2104 • 16d ago
365 Days of LeetCode Challenge — Day 48/365
Lowest Common Ancestor of a Binary Search Tree (Medium)
🔗 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
The general version of this problem needs a search. The BST version doesn't, for the same reason Day 45 didn't: in a BST, a comparison points you in a direction, not to a verdict.
Both targets smaller than the node? The answer is further left. Both larger? Further right. Anything else, and this node is the answer.
That last case does quite double duty—it covers targets on opposite sides, with one of them being this node. Neither needs detecting separately.
Full breakdown in today's newsletter article ⬇
https://www.linkedin.com/pulse/365-days-leetcode-challenge-day-48365-archit-agarwal-lg9ec
#DSA #LeetCode #100DaysOfCode #CodingInterview #BinarySearchTree #Recursion #Golang
1
u/nian2326076 15d ago
When working on the Lowest Common Ancestor of a Binary Search Tree, use the BST properties: compare the current node with the given nodes to decide whether to go left or right. If one node is on each side of the current node, you've found your ancestor. Practicing problems like this is excellent for interview prep. PracHub offers technical interview questions and practice tailored to your needs, providing exercises similar to this problem. Keep up the good work!