Trees: Is This a Binary Search Tree?

  • + 0 comments

    I used the same approach, using the inorder traversal. But in my case I've used an instance of a class for storing the last value. Is it a good practise to do this?

    class staticVar:
        last = None
    
    variable = staticVar()
    
    def checkBST(root):
        if root != None:
            if not checkBST(root.left):
                return False
    
            if variable.last != None and variable.last.data >= root.data:
                return False
    
            variable.last = root
            return checkBST(root.right)
            
        return True