1

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements:

The value of every node in a node's left subtree is less than the data value of that node. The value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a binary search tree?

Complete the function in your editor below, which has parameter: a pointer to the root of a binary tree. It must return a boolean denoting whether or not the binary tree is a binary search tree. You may have to write one or more helper functions to complete this challenge.

Input Format

You are not responsible for reading any input from stdin. Hidden code stubs will assemble a binary tree and pass its root node to your function as an argument.

Constraints: 0<=data<=10^4

Output Format

You are not responsible for printing any output to stdout. Your function must return true if the tree is a binary search tree; otherwise, it must return false. Hidden code stubs will print this result as a Yes or No answer on a new line.

My Code:

""" Node is defined as
class node:
  def __init__(self, data):
      self.data = data
      self.left = None
      self.right = None
"""
def check_binary_search_tree_(root):
    if root is None or (root.left is None and root.right is None):
        return True
    if root.left.data>=root.data or root.right.data<=root.data:
        return False
    check_binary_search_tree_(root.left)
    check_binary_search_tree_(root.right)
    return True

Why am I getting Wrong Answer?

2
  • The problem says: "The value of every node in a node's left subtree is less than the data value of that node. The value of every node in a node's right subtree is greater than the data value of that node." Your code uses <= and >=. Could be a problem. Commented Nov 2, 2020 at 18:48
  • I removed the = and it's still not working Commented Nov 2, 2020 at 18:50

2 Answers 2

3

The problem with your code is

  • first you didn't do :

    return check_binary_search_tree_(root.left) and check_binary_search_tree_(root.right)

  • next even if you do that, you are forgetting to keep the root's value in mind while checking the BST property for left and right children. It could be that your left child is totally a good BST but it fails to be a BST when you consider its parent. Look at the below example:

                  6
              4       7
           2     8
    

    The subtree rooted at 4 is a good BST but fails when you consider its root's value of 6.

  • The solution is then to check the proper range of values at each node i.e.

    left_limit < root.data < right_limit

You could write your function as :

def check_binary_search_tree_(root, min = -math.inf, max = math.inf):
    if root is None:
        return True
    if root.data > min and root.data < max:
        return check_binary_search_tree_(root.left, min, root.data) and check_binary_search_tree_(root.right, root.data, max)
    return False
Sign up to request clarification or add additional context in comments.

1 Comment

According to hackerrank.com/challenges/is-binary-search-tree/problem the parent is irrelevant. Only sub-trees (children) should be considered. but it is implied in the word "every" in "...value of every node in a node's left subtree...". IOW, each root needs to verify that all of its grandchildren/offsprings do not violate the BST criteria.
0
check_binary_search_tree_(root.left)
check_binary_search_tree_(root.right)

You don't return a result of these recursive functions. Try to do:

return check_binary_search_tree_(root.left) and check_binary_search_tree_(root.right)

Full code example that works:

def check_binary_search_tree_(root):
    def isValid(node, lower=float('-inf'), upper=float('inf')):
        if not node:
            return True
        if node.data <= lower or node.data >= upper:
            return False
        return isValid(node.left, lower, node.data) and isValid(node.right, node.data, upper)
    return isValid(root)

1 Comment

That's not the only problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.