Is this tree a binary search tree?
NickName:user2645029 Ask DateTime:2017-07-03T22:14:50

Is this tree a binary search tree?

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)

Copyright Notice:Content Author:「user2645029」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/44887838/is-this-tree-a-binary-search-tree

More about “Is this tree a binary search tree?” related questions

Binary Tree, Binary Search Tree, Binary search

I am trying to understand the Binary tree and referring to online material , and questions asked in SO. I understood that: Binary tree -> Tree in which each node can have at most 2 nodes. Binary

Show Detail

Is this tree a binary search tree?

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

Show Detail

Binary Tree/ Binary Search Tree

Can a Binary Tree / Binary Search Tree only have the parent and one of the left or right nodes? Or is it mandatory to have both the left and right nodes?

Show Detail

convert a not-full binary search tree to full binary search tree

I have a trouble in full binary search tree How can I convert a a not-full binary search tree to full binary search tree, such as this example: I have a structure of Node in tree: struct Node { ...

Show Detail

Binary tree to Binary Search Tree (BST)

How can you convert Binary Tree to Binary Search Tree with O(1) extra space ?

Show Detail

Largest Binary Search Tree in a Binary Tree

So, the question is to find the largest sub-tree (the largest sub-tree is the node with maximum size) in a binary tree which is a BST. I found the following website which enlists an algorithm. ht...

Show Detail

What is the difference between a balanced binary search tree and a binary search tree?

Sorry if this is an extremely basic question, but I am fairly new to trees and hence, this doubt bothers me these days. What is the difference between a binary search tree and a balanced binary se...

Show Detail

path to node in binary search tree as binary search tree

I'm writing a binary search tree implementation, and I want to have a function that finds a node and returns a doubly linked list of all the nodes in the path it took to get there. I know that a do...

Show Detail

Binary search vs binary search tree

What is the benefit of a binary search tree over a sorted array with binary search? Just with mathematical analysis I do not see a difference, so I assume there must be a difference in the low-level

Show Detail

Is there a way to convert from a general tree to binary SEARCH tree?

I know how to convert from a general tree to a binary tree just fine, a a / | \ / b c d -&gt; b \ c ...

Show Detail