/
Solution.java
34 lines (31 loc) · 897 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package g0601_0700.s0700_search_in_a_binary_search_tree;
// #Easy #Tree #Binary_Tree #Binary_Search_Tree #Data_Structure_I_Day_13_Tree
// #2022_03_22_Time_0_ms_(100.00%)_Space_52.3_MB_(6.38%)
import com_github_leetcode.TreeNode;
/*
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while (root != null && root.val != val) {
if (root.val > val) {
root = root.left;
} else {
root = root.right;
}
}
return root;
}
}