Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created March 1, 2021 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/5a8428215e93a27b9522d7d969e5f891 to your computer and use it in GitHub Desktop.
Save amankharwal/5a8428215e93a27b9522d7d969e5f891 to your computer and use it in GitHub Desktop.
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

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