Lowest Common Ancestor of a Binary Search Tree - Complete Solution Guide
Lowest Common Ancestor of a Binary Search Tree is LeetCode problem 235, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST. According to the definition of LCA on Wikipedia : “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself ).” Example 1: Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 Output: 6 Explanation: The LCA of nodes 2 and 8 is 6. Example 2: Input: root =
Detailed Explanation
The problem asks us to find the Lowest Common Ancestor (LCA) of two given nodes, 'p' and 'q', in a Binary Search Tree (BST). The LCA is defined as the lowest node in the tree that has both 'p' and 'q' as descendants (where a node can be a descendant of itself). A BST has the property that for any node, all nodes in its left subtree have values less than the node's value, and all nodes in its right subtree have values greater than the node's value.
Solution Approach
The provided solution uses an iterative approach to find the LCA. Starting from the root, it traverses the tree based on the values of 'p' and 'q' relative to the current node. If both 'p' and 'q' are greater than the current node's value, it moves to the right subtree. If both are less than the current node's value, it moves to the left subtree. If the current node's value is between 'p' and 'q', or if one of 'p' or 'q' is equal to the current node, then the current node is the LCA.
Step-by-Step Algorithm
- Step 1: Initialize 'current' to the root of the BST.
- Step 2: While 'current' is not null, compare the values of 'p' and 'q' to the value of 'current'.
- Step 3: If both 'p.val' and 'q.val' are greater than 'current.val', move 'current' to the right child ('current = current.right').
- Step 4: If both 'p.val' and 'q.val' are less than 'current.val', move 'current' to the left child ('current = current.left').
- Step 5: Otherwise, 'current' is the LCA. Return 'current'.
- Step 6: If the loop finishes without finding an LCA (which shouldn't happen in a valid BST as per problem constraints), return null.
Key Insights
- Insight 1: The BST property is crucial for an efficient solution. It allows us to determine whether 'p' and 'q' are in the left or right subtree of a node without searching the entire tree.
- Insight 2: If both 'p' and 'q' are greater than the current node, their LCA must be in the right subtree. If both are less than the current node, their LCA must be in the left subtree. If the current node's value is between 'p' and 'q', then the current node is the LCA.
- Insight 3: The iterative approach leverages the BST properties to find the LCA by traversing the tree without recursion, leading to constant space complexity.
Complexity Analysis
Time Complexity: O(h)
Space Complexity: O(1)
Topics
This problem involves: Tree, Depth-First Search, Binary Search Tree, Binary Tree.
Companies
Asked at: Google, LinkedIn, Oracle, Samsung, X, Yandex.