Check if a binary tree is binary search tree or not

preview_player
Показать описание
See complete series on data structures here:

In this lesson, we have written a program in C/C++ to verify whether a given binary tree is binary search tree or not.

Рекомендации по теме
Комментарии
Автор

At 14:49, it should be root->data > minValue && root->data < maxValue.
Anyway another great video.

manakupadhyay
Автор

First approach is naive. But it gives an awesome understanding of how recursion works in trees for a complete beginner. The O(n) approach uses range concept which exploits the beautiful properties of a BST.

sumitsaini
Автор

Another approach could be storing the inorder traversal of BT in a temp array and check if the array is sorted in increasing order. If yes, then its a BST.

mrmca
Автор

I like how you went in detail with each recursive call. Great video bro!

allenllewellynkra
Автор

In case this helps others:---
The last approach will not work (in some test cases) if the tree contains INT_MAX or INT_MIN (i.e. 2147483647 or -2147483648)
In cases like (following are tree representations) :
[2147483647]
[-2147483648, null, 2147483647]
[2147483647, 2147483647]

codestorywithMIK
Автор

Correction in one of the code snippet at 14:42
if(root->data < minValue && root->data > maxValue... CORRECTION==> if(root->data > minValue && root->data < maxValue...

rahul-patil
Автор

Your explanation is so awesome and understandable. The first approach seems naive but it's so basic and original for a beginner before approaching the second way.

music-on
Автор

super way to teaching, every single tutorial is so crisp and easy to understand.

manishkumar
Автор

A simpler way would be to make an array using Inorder Traversal and check if it is sorted or not... Sorted will mean it is a BST

ambershekhar
Автор

2:10 we have boolean type in C #include <stdbool.h>

chayakumarsedutainment
Автор

The very first thing that came to my mind for knowing if it is a Binary Search Tree or not  was inorder traversal and check whether it is in sorted order or not. =D

ANSHUKUMARanish
Автор

Great job. Every explanation to this problem I've seen so far explains why the code works. This is the only one that explains how to intuitively and iteratively arrive at the solution one step at a time.

pradeepbalasundaram
Автор

There is a glitch in the final code in the if condition,
if(root->data < minValue && root->data > maxValue) && ...)
should be
if(root->data > minValue && root->data < maxValue) && ...)

KumarSadhu
Автор

Code for checking BST by doing inOrder Traversal only:-
bool checkBST(Node* root) {
static int prevData = -999;
static bool flag = true;
if(root){
checkBST(root->left);
if(root->data > prevData && flag==true){
flag = true;
prevData = root->data;
checkBST(root->right);
return flag;
}
else{
flag = false;
return flag;
}

}
return flag;
}

varunmanchanda
Автор

inorder traversal of a tree will give values in increasing order if it is BST. Have one variable as prev value to compare with current value, return false if prev<=current, else return true. Much easier to implement, with less function calls.

rplusgdj
Автор

In case, you have duplicates in your tree use root->data >= min && root->data <= max.

cRAYonhere
Автор

Please upload videos on interview questions like dynamic programming

deepakjain
Автор

We can also have in-order traversal and check whether the current data is greater than or equal to previous data.

mohitnikumbh
Автор

I understand the that we have written the base case for the recursion is,
if(root == NULL) return true;

But what if the tree is actually NULL, then it'll not check if it's a BST or not it'll simple return true even though there's no Node in the tree.

madanmohan
Автор

The best video on youtube for data structures

ajaydattatray