Trees: Is This a Binary Search Tree?

  • + 3 comments

    I like the approach with inorder traverse. But we have to check not only the order, but also that there are no duplicates. I used "set" for that:

    list(set(my_list))
    

    Here is my code:

    def checkBST(root):
        list_ = []
        def inorder(node):
            if node.data:
                if node.left:
                    inorder(node.left)
                list_.append(node.data)
                if node.right:
                    inorder(node.right)
            return list_
        inorder_list = inorder(root)
        return sorted(list(set(inorder_list))) == inorder_list