1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00

cs: add “700. Search in a Binary Search Tree”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2024-01-06 22:17:54 +01:00
parent 641aa6c529
commit 0178e14f48
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,29 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode SearchBST(TreeNode root, int val) {
if (root == null) {
return null;
}
if (root.val == val) {
return root;
}
if (val < root.val) {
return SearchBST(root.left, val);
}
return SearchBST(root.right, val);
}
}