1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-20 01:56:57 +02:00
LeetCode/cs/search-in-a-binary-tree.cs
Matej Focko 0178e14f48
cs: add “700. Search in a Binary Search Tree”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2024-01-06 22:17:54 +01:00

29 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);
}
}