-2

I am trying to solve a binary search tree problem, but I can't pass all of the test cases. I need to return true if the tree is a binary search tree, otherwise, I need to return false. I also need to check for duplicates and ensure that every value in the right tree is greater than the root, and that every value in left tree is smaller than the root.

This is the hackerrank challenge that I am trying to solve, the link is here: https://www.hackerrank.com/challenges/ctci-is-binary-search-tree

As it was suggested in my fist question, here, Is tree a binary search tree? this is the solution which doesn't check for duplicates or whether every value in the right tree is greater than the root, and similarly for the left tree.

For duplicates, I have an idea how to solve it, but not sure how to check whether values are smaller than root on the left tree and bigger on the right tree.

'''
class node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
'''

def checkBST(root):
    if root == None or (root.left == None and root.right == None):
        return True

    elif root.right == None:
        return root.left.data < root.data and checkBST(root.left)

    elif root.left == None:
        return root.right.data >= root.data and checkBST(root.right)

    return checkBST(root.left) and checkBST(root.right)
  • so you want us to solve this hackerrank problem and give you the code? – Arpit Solanki Jul 03 '17 at 14:18
  • Possible duplicate of [Difference between binary tree and binary search tree](https://stackoverflow.com/questions/6380231/difference-between-binary-tree-and-binary-search-tree) – Arpit Solanki Jul 03 '17 at 14:18
  • I don't care if it is Hackerrank or whatever, I am trying to solve a binary search tree problem that I tried in many iterations and stuck on checking for smaller values on the left tree and bigger on the right tree. I am running it on hackerrank because they have good test cases. And no, this is not a duplicate of https://stackoverflow.com/questions/6380231/difference-between-binary-tree-and-binary-search-tree – user2645029 Jul 03 '17 at 14:30
  • This is a code dump with a phrase that i can not solve this problem please solve this for me – Arpit Solanki Jul 03 '17 at 14:32

1 Answers1

0

Clear, concise and efficient JAVA code: Recursion is cool if you actually understand the phenomenon. The idea is to validate each node of the tree as such that it is always between the min and max value. Start with Integer.MIN_VALUE and Integer.MAX_VALUE as the initial input for min and max.

public boolean isBinarySearch(Node root, int min, int max) {

    if (root == null)
        return true;

    return ((min <= root.val && root.val <= max) && (isBinarySearch(
            root.left, min, root.val) && isBinarySearch(root.right,
            root.val, max)));
} 

You can try this out also.

class Node {

public Node left;
public Node right;
public int val;

public Node(int val) {
    this.val = val;
 }
}

Now do this to run the method.

    Node root2 = new Node(12);
    root2.left = new Node(7);
    root2.left.left = new Node(4);
    root2.left.right = new Node(11);
    root2.right = new Node(16);
    root2.right.left = new Node(14);
    root2.right.right = new Node(18);
    root2.right.right.left = new Node(17);

    System.out
            .println("IsBinary="
                    + wc.isBinarySearch(root2, Integer.MIN_VALUE,
                            Integer.MAX_VALUE));
}
neeranzan
  • 123
  • 5