-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path700 Search in a Binary Search Tree.py
62 lines (51 loc) · 1.66 KB
/
700 Search in a Binary Search Tree.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
root1 = [4,2,7,1,3]
val1 = 2
root2 = [4,2,7,1,3]
val2 = 5
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __repr__(self):
# Shrink the long json string by removing unnecessary lines
t = '\n'.join([l for l in self.json_repr.split('\n')
if '[' not in l and ']' not in l])
return t.replace(',','')
@property
def json_repr(self):
if self.val is None:
return 'None'
# Recursively construct a json-compliant representation
if self.left is None:
text = f"[{self.val}]"
else:
text = f"[{self.val}, [{self.left.json_repr}, {self.right.json_repr}]]"
return json.dumps(json.loads(text), indent=1)
def from_list(l):
nodes = [TreeNode(v) for v in l]
kids = nodes[::-1]
root = kids.pop()
for node in nodes:
if node:
if kids:
node.left = kids.pop()
if kids:
node.right = kids.pop()
return root
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return None
if val > root.val:
return self.searchBST(root.right, val)
elif val < root.val:
return self.searchBST(root.left, val)
else:
return root
# th = TreeNode.from_list(root1)
# print(th)
test = Solution()
print(test.searchBST(TreeNode.from_list(root1), val1))
print(test.searchBST(TreeNode.from_list(root2), val2))