function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
if (p.val < root.val && root.val > q.val && root.left) {
return lowestCommonAncestor(root.left, p, q);
} else if (p.val > root.val && root.val < q.val && root.right) {
return lowestCommonAncestor(root.right, p, q);
} else {
return root;
}
};" but there it's a better way to rewrite it? Or easier? Or using variables?
Assistant
Certainly! Your current implementation works correctly for finding the lowest common ancestor (LCA) in a binary search tree (BST). However, it can be made more readable and easier to understand by simplifying the conditions and using variables to store repeated values. Let's rewrite your function with these improvements:
typescript
function lowestCommonAncestor(
root: TreeNode | null,
p: TreeNode | null,
q: TreeNode | null
): TreeNode | null {
if (!root || !p || !q) return null;
const rootVal = root.val;
const pVal = p.val;
const qVal = q.val;
if (pVal < rootVal && qVal < rootVal) {
// Both nodes are in the left subtree
return lowestCommonAncestor(root.left, p, q);
} else if (pVal > rootVal && qVal > rootVal) {
// Both nodes are in the right subtree
return lowestCommonAncestor(root.right, p, q);
} else {
// The current node is the split point, so it's the LCA
return root;
}
}
Explanation:
Use of Variables: We introduce rootVal, pVal, and qVal variables to store the values of root, p, and q. This makes the code cleaner and avoids repetitive property access.
Simplified Conditions: Instead of checking complex conditions like (p.val < root.val && root.val > q.val), we simplify them by checking if both pVal and qVal are less than or greater than rootVal.
Handling Null Cases: We add a check at the beginning to handle cases where root, p, or q might be null.
Logic:
Left Subtree Recursion: If both pVal and qVal are less than rootVal, it means both nodes are in the left subtree.
Right Subtree Recursion: If both pVal and qVal are greater than rootVal, both nodes are in the right subtree.
Lowest Common Ancestor Found: If pVal and qVal are on different sides of rootVal, or one of them equals rootVal, then root is the LCA.
Why This Works:
In a BST, for any node root, all nodes in the left subtree have values less than root.val, and all nodes in the right subtree have values greater than root.val. The LCA is the node where p and q split into different subtrees.
Alternative Iterative Solution:
If you're interested, here's an iterative version of the same logic:
typescript
function lowestCommonAncestor(
root: TreeNode | null,
p: TreeNode | null,
q: TreeNode | null
): TreeNode | null {
if (!p || !q) return null;
let current = root;
while (current) {
const rootVal = current.val;
const pVal = p.val;
const qVal = q.val;
if (pVal < rootVal && qVal < rootVal) {
current = current.left;
} else if (pVal > rootVal && qVal > rootVal) {
current = current.right;
} else {
return current;
}
}
return null;
}
Benefits of the Iterative Approach:
Space Efficiency: It avoids the extra space used by the recursive call stack.
Performance: May perform better for very deep trees where recursion might lead to a stack overflow.
Summary:
Simplify conditions by comparing pVal and qVal directly to rootVal.
Use variables to make the code more readable.
Handle null cases to make the function more robust.
Consider an iterative approach if appropriate for your use case.
Let me know if you have any questions or need further clarification!