-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
700.py
46 lines (40 loc) · 1.37 KB
/
700.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
__________________________________________________________________________________________________
sample 68 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if val == root.val:
return root
if val < root.val:
if root.left:
return self.searchBST(root.left, val)
return None
if root.right:
return self.searchBST(root.right, val)
return None
__________________________________________________________________________________________________
sample 15128 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return []
trav = root
while trav:
if trav.val == val:
return trav
elif trav.val < val:
trav = trav.right
else:
trav = trav.left
__________________________________________________________________________________________________