# Finding if a Binary Tree is a Binary Search Tree

Filed under:
|
|
##### binary-search-tree

Today I had an interview where I was asked to write a program which takes a Binary Tree and returns true if it is also a Binary Search Tree otherwise false.

My Approach1: Perform an inroder traversal and store the elements in O(n) time. Now scan through the array/list of elements and check if element at ith index is greater than element at (i+1)th index. If such a condition is encountered, return false and break out of the loop. (This takes O(n) time). At the end return true.

But this gentleman wanted me to provide an efficient solution. I tried but I was unsuccessfult, because to find if it is a BST I have to check each node.

Moreover he was pointing me to think over recusrion. My Approach 2: A BT is a BST if for any node N N->left is < N and N->right > N , and the INorder successor of left node of N is less than N and the inorder successor of right node of N is greater than N and the left and right subtrees are BSTs.

But this is going to be complicated and running time doesn't seem to be good. Please help if you know any optimal solution.

Thanks.

© Stack Overflow or respective owner

• #### Jpeg Algorithm vs BMP Algorithm?

as seen on Super User - Search for 'Super User'
I'm just wonder, what the differences are between creating a BMP file algorithm and JPG file algorithm ? If you know the others images' format algorithm, please post them. Thanks. >>> More

• #### word disambiguation algorithm (Lesk algorithm)

as seen on Stack Overflow - Search for 'Stack Overflow'
Hii.. Can anybody help me to find an algorithm in Java code to find synonyms of a search word based on the context and I want to implement the algorithm with WordNet database. For example, "I am running a Java program". From the context, I want to find the synonyms for the word "running", but the… >>> More

• #### Search algorithm (with a sort algorithm already implemented)

as seen on Stack Overflow - Search for 'Stack Overflow'
Hello, Im doing a Java application and Im facing some doubts in which concerns performance. I have a PriorityQueue which guarantees me the element removed is the one with greater priority. That PriorityQueue has instances of class Event (which implements Comparable interface). Each Event is associated… >>> More

• #### Is there any algorithm for finding LINES by PIXEL COLORS on picture?

as seen on Stack Overflow - Search for 'Stack Overflow'
So I have Image like this I want to get something like this (I hevent drawn all lines I want but I hope you can get my idea) I need algorithm for finding all straight lines on it by just reading colors of pixels. No hard math, no Haar, no Hough. Some algorithm which would be based on points… >>> More

• #### collsion issues with quadtree [on hold]

as seen on Game Development - Search for 'Game Development'
So i implemented a Quad tree in Java for my 2D game and everything works fine except for when i run my collision detection algorithm, which checks if a object has hit another object and which side it hit.My problem is 80% of the time the collision algorithm works but sometimes the objects just go… >>> More

• #### finding two most distant elements in a binary tree

as seen on Stack Overflow - Search for 'Stack Overflow'
I am looking for an algorithm that could find the two most distant elements in a binary tree, not looking for any special language, just for the algorithm. Thanks. >>> More

• #### Binary tree to get minimum element in O(1)

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm accessing the minimum element of a binary tree lots of times. What implementations allow me to access the minimum element in constant time, rather than O(log n)? >>> More

• #### Binary Tree in C Insertion Error

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm quite new to C and I'm trying to implement a Binary Tree in C which will store a number and a string and then print them off e.g. 1 : Bread 2 : WashingUpLiquid etc. The code I have so far is: #include <stdio.h> #include <stdlib.h> #define LENGTH 300 struct node { int data; … >>> More

• #### Simple Fundamental Algorithm Question, binary tree traversal

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm trying to explain to non-computer science major student with many questions. (1)What traverses tree? Is it just logic or actual on off switch generates 1s and 0s traveling the circuit board? where is this tree and node exists CPU/Memory in between? (2)If it is 1s and 0s HOW the circuits understand… >>> More

• #### How to create a Binary Tree from a General Tree?

as seen on Stack Overflow - Search for 'Stack Overflow'
I have to solve the following constructor for a BinaryTree class in java: BinaryTree(GeneralTree<T> aTree) This method should create a BinaryTree (bt) from a General Tree (gt) as follows: Every Vertex from gt will be represented as a leaf in bt. If gt is a leaf, then bt will be a leaf… >>> More