cs: add “700. Search in a Binary Search Tree”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
641aa6c529
commit
0178e14f48
1 changed files with 29 additions and 0 deletions
29
cs/search-in-a-binary-tree.cs
Normal file
29
cs/search-in-a-binary-tree.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue