Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class binarytree:
def __init__(self, val):
self.val = val
self.leftnode = leftnode
self.rightnode = rightnode
import sys
class BinarySearchTree:
def validate_BST(self, root: binarytree) -> bool:
return self.valid(root, sys.maxsize, -sys.maxsize)
def valid(self, root, max_, min_):
if root == None:
return True
else:
return False
return self.valid(root.leftnode, root.val, min_) and self.valid(root.rightnode, max_, root.val)
@AIbootcamp
Copy link

AIbootcamp commented Oct 11, 2021

Hi Aman, thank you for the 120 Python Projects. They are highly encouraging to keep learning.

In this solution there is no validation on the value structure of the Binary tree: left < root < right.

I believe the solution I propose works

    def valid(self, root, max_, min_):
        if root == None:
            return True
        else:
            if root.val > min_ and root.val < max_:
                return self.valid(root.leftnode, root.val, min_) and self.valid(root.rightnode, max__, root.val)
            else:
                return False

Thank you again por posting the projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment