Skip to content

Commit 0e1c019

Browse files
committedDec 22, 2020
feat: search-in-a-binary-search-tree
1 parent 9c88a26 commit 0e1c019

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
 

‎bst.search-in-a-binary-search-tree.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
class Solution:
9+
"""
10+
700. 二叉搜索树中的搜索
11+
https://leetcode-cn.com/problems/search-in-a-binary-search-tree/
12+
给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。
13+
"""
14+
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
15+
if not root:
16+
return None
17+
18+
if root.val > val:
19+
return self.searchBST(root.left, val)
20+
21+
if root.val < val:
22+
return self.searchBST(root.right, val)
23+
24+
return root
25+
26+

0 commit comments

Comments
 (0)