Skip to content

Commit c01c8e9

Browse files
committed
ported BST validator
1 parent 5378f08 commit c01c8e9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

BST_Validator/main.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
3+
class Node:
4+
def __init__(self,value=None):
5+
self.value=value
6+
self.left_child=None
7+
self.right_child=None
8+
9+
def validate_bst(root,min=-sys.maxsize,max=sys.maxsize):
10+
if root==None:
11+
return True
12+
if (root.value>min and
13+
root.value<max and
14+
validate_bst(root.left_child,min,root.value) and
15+
validate_bst(root.right_child,root.value,max)):
16+
return True
17+
else:
18+
return False
19+
20+
root=Node(5)
21+
l1=Node(4)
22+
r1=Node(6)
23+
r2=Node(10)
24+
25+
r1.right_child=r2
26+
27+
root.left_child=l1
28+
root.right_child=r1
29+
30+
print(validate_bst(root))

0 commit comments

Comments
 (0)