Lowest Common Ancestor of a Binary Search Tree in BST

Day 82/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Lowest Common Ancestor of a Binary Search Tree A clean problem that shows the power of BST properties. Problem idea: Find the lowest common ancestor (LCA) of two nodes in a BST. Key idea: Use BST ordering to decide direction (no need to explore entire tree) Why? • In a BST: left < root < right • If both nodes are smaller → go left • If both nodes are greater → go right • Otherwise → current node is the LCA How it works: • Start from root • If p and q are both < root → move left • If p and q are both > root → move right • Else → you’ve found the split point (LCA) Time Complexity: O(h) Space Complexity: O(1) (iterative approach) Big takeaway: Whenever working with BST, always use its ordering property to optimize traversal. 🔥 This avoids unnecessary recursion and makes the solution super efficient. Day 82 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearchTree #TreeTraversal #LCA #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories