Repository navigation
Add solution for the "Lowest Common Ancestor of a Binary Search Tree" problem with unit tests. #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||||||||||||||||||||||||||||||
| // Tags: Tree, DFS, Binary Search Tree | ||||||||||||||||||||||||||||||||||
| package com.leetcode.easy; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import com.leetcode.datastructure.TreeNode; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public class LowestCommonAncestorOfABinarySearchTree235 { | ||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| Time complexity: O(N), | ||||||||||||||||||||||||||||||||||
| where N is the number of nodes in the BST. | ||||||||||||||||||||||||||||||||||
| In the worst case we might be visiting all the nodes of the BST. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Space complexity: O(N). | ||||||||||||||||||||||||||||||||||
| This is because the maximum amount of space | ||||||||||||||||||||||||||||||||||
| utilized by the recursion stack would be N | ||||||||||||||||||||||||||||||||||
| since the height of a skewed BST could be N. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||||||||||||||||||||||||||||||||||
| if (p.val > root.val && q.val > root.val) { | ||||||||||||||||||||||||||||||||||
| return this.lowestCommonAncestor(root.right, p, q); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| if (p.val < root.val && q.val < root.val) { | ||||||||||||||||||||||||||||||||||
| return this.lowestCommonAncestor(root.left, p, q); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+19
to
+22
|
||||||||||||||||||||||||||||||||||
| return this.lowestCommonAncestor(root.right, p, q); | |
| } | |
| if (p.val < root.val && q.val < root.val) { | |
| return this.lowestCommonAncestor(root.left, p, q); | |
| if (root.right != null) { | |
| return this.lowestCommonAncestor(root.right, p, q); | |
| } else { | |
| return null; | |
| } | |
| } | |
| if (p.val < root.val && q.val < root.val) { | |
| if (root.left != null) { | |
| return this.lowestCommonAncestor(root.left, p, q); | |
| } else { | |
| return null; | |
| } |
Copilot
AI
Sep 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method doesn't handle null values for root, p, or q parameters. If any of these are null, accessing .val will throw a NullPointerException. Consider adding null checks at the beginning of the method.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.leetcode.easy; | ||
|
|
||
| import com.leetcode.datastructure.TreeNode; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class LowestCommonAncestorOfABinarySearchTree235Test { | ||
| private LowestCommonAncestorOfABinarySearchTree235 solution; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| solution = new LowestCommonAncestorOfABinarySearchTree235(); | ||
| } | ||
|
|
||
| @Test | ||
| void testLowestCommonAncestor_Example1() { | ||
| TreeNode root = new TreeNode(6, | ||
| new TreeNode(2, new TreeNode(0), new TreeNode(4, new TreeNode(3), new TreeNode(5))), | ||
| new TreeNode(8, new TreeNode(7), new TreeNode(9))); | ||
| TreeNode p = root.left; // Node with value 2 | ||
| TreeNode q = root.right; // Node with value 8 | ||
| TreeNode result = solution.lowestCommonAncestor(root, p, q); | ||
| assertEquals(root, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testLowestCommonAncestor_Example2() { | ||
| TreeNode root = new TreeNode(6, | ||
| new TreeNode(2, new TreeNode(0), new TreeNode(4, new TreeNode(3), new TreeNode(5))), | ||
| new TreeNode(8, new TreeNode(7), new TreeNode(9))); | ||
| TreeNode p = root.left; // Node with value 2 | ||
| TreeNode q = root.left.right; // Node with value 4 | ||
| TreeNode result = solution.lowestCommonAncestor(root, p, q); | ||
| assertEquals(p, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testLowestCommonAncestor_Example3() { | ||
| TreeNode root = new TreeNode(2, new TreeNode(1), null); | ||
| TreeNode p = root; // Node with value 2 | ||
| TreeNode q = root.left; // Node with value 1 | ||
| TreeNode result = solution.lowestCommonAncestor(root, p, q); | ||
| assertEquals(root, result); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential NullPointerException if root.right is null. The method should check if root.right exists before making the recursive call.