Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions src/LowestCommonAncestorOfaBinarySearchTree235.java

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

Copilot AI Sep 15, 2025

Copy link

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.

Suggested change
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 uses AI. Check for mistakes.
Comment on lines +19 to +22

Copilot AI Sep 15, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential NullPointerException if root.left is null. The method should check if root.left exists before making the recursive call.

Suggested change
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 uses AI. Check for mistakes.
}
return root;
}
Comment on lines +17 to +25

Copilot AI Sep 15, 2025

Copy link

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.

Copilot uses AI. Check for mistakes.
}
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);
}
}