mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 18:07:32 +01:00
30 lines
681 B
C#
30 lines
681 B
C#
|
/**
|
||
|
* 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);
|
||
|
}
|
||
|
}
|