forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.py
29 lines (23 loc) · 710 Bytes
/
sol.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
from tree import Node
import random
class TreeNode(Node):
def __init__(self, val=None):
super().__init__(data=val)
def isValidBST(root):
if root is None:
return True
if root.left is not None:
c1 = root.left.data<=root.data
else:
c1 = True
if root.right is not None:
c2 = root.right.data>=root.data
else:
c2 = True
return c1 and c2 and isValidBST(root.left) and isValidBST(root.right)
def main():
tree = TreeNode(11); tree.left=TreeNode(10); tree.left.right=TreeNode(10.5); tree.right=TreeNode(12); tree.right.left=TreeNode(11.5)
tree.prettyPrint()
print('\n\n', isValidBST(tree))
if __name__=='__main__':
main()