The Time Limit Exceeded (TLE) error in your LeetCode submission is likely due to a small but crucial mistake in your code. Let's walk through the issue and how to fix it.
Issue in Your Code
Here's the problematic part of your code:
while (cur != null) {
if (p.val < cur.val && q.val < cur.val) {
cur = root.left; // Should be cur.left
} else if (p.val > cur.val && q.val > cur.val) {
cur = root.right; // Should be cur.right
} else {
return cur;
}
}
What's Wrong?
-
Incorrect Pointer Update: Inside the while loop, you're updating cur to root.left or root.right instead of cur.left or cur.right. This means that regardless of where you are in the tree, you're always moving from the original root, which can cause an infinite loop if the root keeps getting reassigned incorrectly.
-
Infinite Loop Scenario: If both p and q are less than root.val, you set cur = root.left. However, in the next iteration, you again check against root.val instead of the new cur.val. This prevents cur from moving down the tree properly, leading to an infinite loop and thus a TLE.
How to Fix It
You should update cur relative to its current value, not always starting from root. Here's the corrected code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode cur = root;
while (cur != null) {
if (p.val < cur.val && q.val < cur.val) {
cur = cur.left; // Corrected from root.left to cur.left
} else if (p.val > cur.val && q.val > cur.val) {
cur = cur.right; // Corrected from root.right to cur.right
} else {
return cur;
}
}
return null;
}
}
Why This Works
-
Proper Traversal: By updating cur to cur.left or cur.right, you ensure that you're moving down the tree from the current node, which is essential for correctly finding the lowest common ancestor.
-
Termination Guarantee: This change ensures that the loop progresses towards the potential LCA, preventing infinite loops and thus avoiding the TLE error.
Additional Tips
-
Edge Cases: Ensure that p and q are present in the tree. While LeetCode problems typically guarantee this, it's good practice to handle such scenarios if you're writing production code.
-
Tree Type: This solution assumes that the tree is a Binary Search Tree (BST). If you're dealing with a general binary tree, you'll need a different approach, such as using recursion to traverse both subtrees.
-
Testing: After making the changes, test your code with various scenarios, including:
p and q are on different sides of the root.
- One is the ancestor of the other.
- Both are in the left or right subtree.
Final Correct Code
Here's the complete corrected version for clarity:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode cur = root;
while (cur != null) {
if (p.val < cur.val && q.val < cur.val) {
cur = cur.left;
} else if (p.val > cur.val && q.val > cur.val) {
cur = cur.right;
} else {
return cur;
}
}
return null;
}
}
With this correction, your solution should run efficiently without exceeding the time limits.