File tree 5 files changed +62
-0
lines changed
algorithms/SearchInABinarySearchTree
5 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -203,6 +203,7 @@ All solutions will be accepted!
203
203
| 22| [ Generate Parentheses] ( https://leetcode-cn.com/problems/generate-parentheses/description/ ) | [ java/py/js] ( ./algorithms/GenerateParentheses ) | Easy|
204
204
| 704| [ Binary Search] ( https://leetcode-cn.com/problems/binary-search/description/ ) | [ java/py/js] ( ./algorithms/BinarySearch ) | Easy|
205
205
| 709| [ To Lower Case] ( https://leetcode-cn.com/problems/to-lower-case/description/ ) | [ java/py/js] ( ./algorithms/ToLowerCase ) | Easy|
206
+ | 700| [ Search In A Binary Search Tree] ( https://leetcode-cn.com/problems/search-in-a-binary-search-tree/description/ ) | [ java/py/js] ( SearchInABinarySearchTree ) | Easy|
206
207
207
208
# Database
208
209
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Search In A Binary Search Tree
2
+ This problem is easy to sovle by recursive
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+ class Solution {
11
+ public TreeNode searchBST (TreeNode root , int val ) {
12
+ if (root == null || root .val == val ) return root ;
13
+ else if (root .val > val ) return searchBST (root .left , val );
14
+ else return searchBST (root .right , val );
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val) {
4
+ * this.val = val;
5
+ * this.left = this.right = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {TreeNode } root
10
+ * @param {number } val
11
+ * @return {TreeNode }
12
+ */
13
+ var searchBST = function ( root , val ) {
14
+ // Should return null, but the acception is []
15
+ if ( ! root ) return [ ]
16
+
17
+ if ( root . val === val ) return root
18
+ else if ( root . val > val ) return searchBST ( root . left , val )
19
+ else return searchBST ( root . right , val )
20
+ } ;
Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ # class TreeNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.left = None
6
+ # self.right = None
7
+
8
+ class Solution (object ):
9
+ def searchBST (self , root , val ):
10
+ """
11
+ :type root: TreeNode
12
+ :type val: int
13
+ :rtype: TreeNode
14
+ """
15
+ if not root :
16
+ return None
17
+
18
+ if root .val == val :
19
+ return root
20
+ elif root .val > val :
21
+ return self .searchBST (root .left , val )
22
+ else :
23
+ return self .searchBST (root .right , val )
You can’t perform that action at this time.
0 commit comments