-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday25.py
More file actions
27 lines (21 loc) · 795 Bytes
/
day25.py
File metadata and controls
27 lines (21 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Treenode:
def __init__(self,val=0,left=None,right=None):
self.val=val
self.left=left
self.right=right
def is_valid_bst(root):
#func validate is given to check if the current node and its subtress satisfy bst
def validate(node,low=float('-inf'),high=float('inf')):
#An empty node means its a valid subtree
if not node:
return True
#The currents nodes value must be within the range[low,high]
if node.val<=low or node.val>=high:
return False
#recursively searching in left & right subtrees with updated ranges
return(validate(node.left,low,node.val) and validate(node.right,node.val,high))
return validate(root)
root =Treenode(2)
root.left=Treenode(1)
root.right=Treenode(3)
print(is_valid_bst(root))