Skip to content

Commit 5f053ae

Browse files
solves search in bnary search tree
1 parent 51d921c commit 5f053ae

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-169/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-169/2081-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-169/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-170/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-170/2081-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-170/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
88

@@ -190,7 +190,7 @@
190190
| 693 | [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits) | [![Java](assets/java.png)](src/BinaryNumberWithAlternatingBits.java) [![Python](assets/python.png)](python/binary_number_with_alternating_bits.py) |
191191
| 696 | [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings) | [![Java](assets/java.png)](src/CountBinarySubstrings.java) [![Python](assets/python.png)](python/count_binary_substrings.py) |
192192
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | [![Java](assets/java.png)](src/DegreeOfAnArray.java) [![Python](assets/python.png)](python/degree_of_an_array.py) |
193-
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree) | |
193+
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree) | [![Java](assets/java.png)](src/SearchInBinarySearchTree.java) [![Python](assets/python.png)](python/search_in_binary_search_tree.py) |
194194
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream) | |
195195
| 704 | [Binary Search](https://leetcode.com/problems/binary-search) | |
196196
| 705 | [Design HashSet](https://leetcode.com/problems/design-hashset) | |
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
14+
if root is None or root.val == val: return root
15+
return self.searchBST(root.right, val) if root.val < val else self.searchBST(root.left, val)

src/SearchInBinarySearchTree.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class SearchInBinarySearchTree {
2+
public TreeNode searchBST(TreeNode root, int val) {
3+
if (root == null || root.val == val) return root;
4+
return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val);
5+
}
6+
}

0 commit comments

Comments
 (0)