Skip to content

Commit

Permalink
Create validate-bintree.py
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedonnan authored Jan 25, 2023
1 parent 3f2c641 commit 547dcdf
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions validate-bintree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
return self.traverse(root, [], [])

def traverse(self, root: Optional[TreeNode], gt: List[int], lt: List[int]) -> bool:
left = True
right = True
for item in lt:
if root.val >= item:
return False
for item in gt:
if root.val <= item:
return False

if root.left != None:
left = self.traverse(root.left, gt, lt + [root.val])
if root.right != None:
right = self.traverse(root.right, gt + [root.val], lt)
return left and right

0 comments on commit 547dcdf

Please sign in to comment.