Trees: Is This a Binary Search Tree?

  • + 0 comments

    I first tried to check every node, but I couldn't figure out how to check the entire tree until I read your code, here's the code I tried first:

    def checkBST(root):
        truth = True
        if(root.left != None):
            truth *= root.left.data < root.data
            truth *= checkBST(root.left)
        if(root.right != None):
            truth *= root.data < root.right.data
            truth *= checkBST(root.right)
        return(truth)
    

    It doesn't work because it doesn't check for duplicates or if leafs are actually in order. Thanks for the elegant solution!