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
18 changes: 0 additions & 18 deletions src/SearchInaBinarySearchTree700.java

This file was deleted.

24 changes: 24 additions & 0 deletions src/main/java/com/leetcode/easy/SearchInABinarySearchTree700.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Tags: Tree, DFS
package com.leetcode.easy;

import com.leetcode.datastructure.TreeNode;

public class SearchInABinarySearchTree700 {
/**
* Time complexity: O(H), where H is a tree height.
* That results in O(logN) in the average case, and O(N) in the worst case.
* <p>
* Space complexity: O(H) to keep the recursion stack,
* i.e. O(logN) in the average case, and O(N) in the worst case.
*/
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) {
return root;
}
if (root.val > val) {
return this.searchBST(root.left, val);
} else {
return this.searchBST(root.right, val);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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;
import static org.junit.jupiter.api.Assertions.assertNull;

public class SearchInABinarySearchTree700Test {
private SearchInABinarySearchTree700 solution;

@BeforeEach
void setUp() {
solution = new SearchInABinarySearchTree700();
}

@Test
void testSearchBST_Example1() {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(7);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
int val = 2;

TreeNode result = solution.searchBST(root, val);

assertEquals(result, root.left);
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

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

The assertEquals parameters are in the wrong order. The convention in JUnit and throughout this codebase is assertEquals(expected, actual), but here it's assertEquals(actual, expected). This should be assertEquals(root.left, result) to match the pattern used in other tests.

Suggested change
assertEquals(result, root.left);
assertEquals(root.left, result);

Copilot uses AI. Check for mistakes.
}

@Test
void testSearchBST_Example2() {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(7);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
int val = 5;

TreeNode result = solution.searchBST(root, val);

assertNull(result);
}
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

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

The test coverage is incomplete. Consider adding tests for important edge cases: 1) null root (should return null), 2) value found at root node (should return root), and 3) value found in right subtree (currently only left subtree is tested). These cases are handled in the implementation but not verified by tests.

Suggested change
}
}
@Test
void testSearchBST_NullRoot() {
TreeNode root = null;
int val = 1;
TreeNode result = solution.searchBST(root, val);
assertNull(result);
}
@Test
void testSearchBST_ValueAtRoot() {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(7);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
int val = 4;
TreeNode result = solution.searchBST(root, val);
assertEquals(root, result);
}
@Test
void testSearchBST_ValueInRightSubtree() {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(7);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
int val = 7;
TreeNode result = solution.searchBST(root, val);
assertEquals(root.right, result);
}

Copilot uses AI. Check for mistakes.
}