Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8795c15

Browse files
authored
Added task 700.
1 parent 41feb6d commit 8795c15

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0601_0700.s0700_search_in_a_binary_search_tree;
2+
3+
// #Easy #Tree #Binary_Tree #Binary_Search_Tree
4+
5+
import com_github_leetcode.TreeNode;
6+
7+
/*
8+
* Definition for a binary tree node.
9+
* public class TreeNode {
10+
* int val;
11+
* TreeNode left;
12+
* TreeNode right;
13+
* TreeNode() {}
14+
* TreeNode(int val) { this.val = val; }
15+
* TreeNode(int val, TreeNode left, TreeNode right) {
16+
* this.val = val;
17+
* this.left = left;
18+
* this.right = right;
19+
* }
20+
* }
21+
*/
22+
public class Solution {
23+
public TreeNode searchBST(TreeNode root, int val) {
24+
while (root != null && root.val != val) {
25+
if (root.val > val) {
26+
root = root.left;
27+
} else {
28+
root = root.right;
29+
}
30+
}
31+
return root;
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
700\. Search in a Binary Search Tree
2+
3+
Easy
4+
5+
You are given the `root` of a binary search tree (BST) and an integer `val`.
6+
7+
Find the node in the BST that the node's value equals `val` and return the subtree rooted with that node. If such a node does not exist, return `null`.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/01/12/tree1.jpg)
12+
13+
**Input:** root = [4,2,7,1,3], val = 2
14+
15+
**Output:** [2,1,3]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/01/12/tree2.jpg)
20+
21+
**Input:** root = [4,2,7,1,3], val = 5
22+
23+
**Output:** []
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range `[1, 5000]`.
28+
* <code>1 <= Node.val <= 10<sup>7</sup></code>
29+
* `root` is a binary search tree.
30+
* <code>1 <= val <= 10<sup>7</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0601_0700.s0700_search_in_a_binary_search_tree;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import com_github_leetcode.TreeUtils;
8+
import java.util.Arrays;
9+
import org.junit.jupiter.api.Test;
10+
11+
class SolutionTest {
12+
@Test
13+
void searchBST() {
14+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 7, 1, 3));
15+
TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3));
16+
assertThat(new Solution().searchBST(root, 2).toString(), equalTo(expected.toString()));
17+
}
18+
19+
@Test
20+
void searchBST2() {
21+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 7, 1, 3));
22+
assertThat(new Solution().searchBST(root, 5), equalTo(null));
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /