Trees: Is This a Binary Search Tree?

  • + 2 comments

    Python 3 version:

    def checkBST(n, min=0, max=10000):
        if not n:
            return True
        if n.data <= min or n.data >= max:
            return False
        return checkBST(n.left, min, n.data) and checkBST(n.right, n.data, max)
    

    I have simplified the above "clean" Java solution to make a singular function. It is inline with my initial algorithm, but less than 1/3 of the code (was a conglomorated mish-mosh of extra checks and functions to perform them).