Trees: Is This a Binary Search Tree?

  • + 1 comment

    I ended up with pretty much the same code but instead of a closure variable I just passed the variable in. I used a single element list since it's gets passed by reference which allows changes to persist when going back down the call stack:

    def checkBST(root):
        return(check_in_order(root,[-1]))
        
    def check_in_order(root,prev):
        result = True
        if root.left is not None:
            result &= check_in_order(root.left,prev)
        if prev[0] >= root.data:
            return False
        prev[0] = root.data
        if root.right is not None:
            result &= check_in_order(root.right,prev)
        return result