Trees: Is This a Binary Search Tree?

  • + 2 comments

    My code is:

    def isBST(root):
        if root.left and root.left.data >= root.data:
            return False
        if root.left:
            print(root.left.data,"<",root.data)
        if root.right and root.right.data <= root.data:
            return False
        if root.right:
            print(root.right.data,">",root.data)
        return True
    
    def checkBST(root):
        if not isBST(root):
            return False
        if root.left:
            checkBST(root.left)
        if root.right:
            checkBST(root.right)
        return True
    

    Input:

    2
    1 2 4 3 5 6 7
    

    Output:

    2 < 3
    6 > 3
    1 < 2
    4 > 2
    5 < 6
    7 > 6
    Yes
    

    Can anyone please where is the problem?