|
| 1 | +--- |
| 2 | +id: lowest-common-ancestor-of-a-binary-search-tree. |
| 3 | +title: Lowest Common Ancestor of a Binary Search Tree. |
| 4 | +sidebar_label: 235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree |
| 5 | +tags: |
| 6 | +- Binary Search Tree |
| 7 | +- Tree |
| 8 | +- Depth-First Search |
| 9 | +- Binary Tree |
| 10 | +description: "Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem |
| 14 | + |
| 15 | +Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST. |
| 16 | + |
| 17 | +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).” |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +**Input:** `root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8` |
| 26 | +**Output:** `6` |
| 27 | +**Explanation:** The LCA of nodes 2 and 8 is 6. |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +**Input:** `root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4` |
| 34 | +**Output:** `2` |
| 35 | +**Explanation:** The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition. |
| 36 | + |
| 37 | +**Example 3:** |
| 38 | + |
| 39 | +**Input:** `root = [2,1], p = 2, q = 1` |
| 40 | +**Output:** `2` |
| 41 | + |
| 42 | +### Constraints |
| 43 | + |
| 44 | +- The number of nodes in the tree is in the range `[2, 10^5]`. |
| 45 | +- `-10^9 <= Node.val <= 10^9` |
| 46 | +- All `Node.val` are unique. |
| 47 | +- `p != q` |
| 48 | +- `p` and `q` will exist in the BST. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## Approach |
| 53 | + |
| 54 | +To find the lowest common ancestor in a Binary Search Tree (BST), we can utilize the properties of the BST. The left subtree of a node contains only nodes with values less than the node's value, and the right subtree contains only nodes with values greater than the node's value. |
| 55 | + |
| 56 | +### Steps: |
| 57 | + |
| 58 | +1. **Start from the Root:** Begin the search from the root node of the BST. |
| 59 | +2. **Value Comparison:** |
| 60 | + - If both `p` and `q` are smaller than the current node's value, move to the left child. |
| 61 | + - If both `p` and `q` are greater than the current node's value, move to the right child. |
| 62 | + - If `p` and `q` lie on either side of the current node, or one of them is the current node, then the current node is their LCA. |
| 63 | + |
| 64 | +### Solution |
| 65 | + |
| 66 | +#### Java |
| 67 | + |
| 68 | +```java |
| 69 | +/** |
| 70 | + * Definition for a binary tree node. |
| 71 | + * public class TreeNode { |
| 72 | + * int val; |
| 73 | + * TreeNode left; |
| 74 | + * TreeNode right; |
| 75 | + * TreeNode(int x) { val = x; } |
| 76 | + * } |
| 77 | + */ |
| 78 | +class Solution { |
| 79 | + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { |
| 80 | + while (root != null) { |
| 81 | + // If both p and q are lesser than root, LCA must be in the left subtree |
| 82 | + if (p.val < root.val && q.val < root.val) { |
| 83 | + root = root.left; |
| 84 | + } |
| 85 | + // If both p and q are greater than root, LCA must be in the right subtree |
| 86 | + else if (p.val > root.val && q.val > root.val) { |
| 87 | + root = root.right; |
| 88 | + } |
| 89 | + // If p and q lie on either side of root, or one of them is the root, then root is the LCA |
| 90 | + else { |
| 91 | + return root; |
| 92 | + } |
| 93 | + } |
| 94 | + return null; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | +#### CPP |
| 99 | +```cpp |
| 100 | +/** |
| 101 | + * Definition for a binary tree node. |
| 102 | + * struct TreeNode { |
| 103 | + * int val; |
| 104 | + * TreeNode *left; |
| 105 | + * TreeNode *right; |
| 106 | + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 107 | + * }; |
| 108 | + */ |
| 109 | +class Solution { |
| 110 | +public: |
| 111 | + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { |
| 112 | + while (root != nullptr) { |
| 113 | + // If both p and q are lesser than root, LCA must be in the left subtree |
| 114 | + if (p->val < root->val && q->val < root->val) { |
| 115 | + root = root->left; |
| 116 | + } |
| 117 | + // If both p and q are greater than root, LCA must be in the right subtree |
| 118 | + else if (p->val > root->val && q->val > root->val) { |
| 119 | + root = root->right; |
| 120 | + } |
| 121 | + // If p and q lie on either side of root, or one of them is the root, then root is the LCA |
| 122 | + else { |
| 123 | + return root; |
| 124 | + } |
| 125 | + } |
| 126 | + return nullptr; |
| 127 | + } |
| 128 | +}; |
| 129 | +``` |
| 130 | +### Python |
| 131 | +```python |
| 132 | +# Definition for a binary tree node. |
| 133 | +class TreeNode: |
| 134 | + def __init__(self, x): |
| 135 | + self.val = x |
| 136 | + self.left = None |
| 137 | + self.right = None |
| 138 | +
|
| 139 | +class Solution: |
| 140 | + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': |
| 141 | + while root: |
| 142 | + # If both p and q are lesser than root, LCA must be in the left subtree |
| 143 | + if p.val < root.val and q.val < root.val: |
| 144 | + root = root.left |
| 145 | + # If both p and q are greater than root, LCA must be in the right subtree |
| 146 | + elif p.val > root.val and q.val > root.val: |
| 147 | + root = root.right |
| 148 | + # If p and q lie on either side of root, or one of them is the root, then root is the LCA |
| 149 | + else: |
| 150 | + return root |
| 151 | + return None |
| 152 | +``` |
| 153 | + |
| 154 | +### Complexity Analysis |
| 155 | +#### Time Complexity: O(h) |
| 156 | + |
| 157 | +**Reason**: The algorithm may traverse the height h of the tree. In the worst case, this is O(log n) for a balanced BST and O(n) for a skewed BST. |
| 158 | +Space Complexity: O(1) |
| 159 | + |
| 160 | +**Reason**: The algorithm uses constant space. |
| 161 | +#### References |
| 162 | +**LeetCode Problem** : Lowest Common Ancestor of a Binary Search Tree |
| 163 | +**Solution Link**: LCA Solution on LeetCode |
| 164 | + |
| 165 | +**Wikipedia Definition**: Lowest Common Ancestor |
0 commit comments