Check tree for BST
PROBLEM :
Given a binary tree, return true if it is BST, else false. For example, the following tree is not BST, because 11 is in left subtree of 10.
10
/ \
7 39
\
11
Input:
The task is to complete the method which takes one argument, root of Binary Tree. The struct Node has a data part which stores the data, pointer to left child and pointer to right child.
There are multiple test cases. For each test case, this method will be called individually.
Output:
The function should return count of leaves
Constraints:
1 <=T<= 30
1 <= Number of nodes<= 100
1 <= Data of a node<= 1000
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* A binary tree node has data, pointer to left child
and a pointer to right child
struct Node {
int data;
struct Node* left, * right;
}; */
/* Should return true if tree represented by root is BST.
For example, return value should be true for following tree.
20
/ \
10 30
and return value should be false for following tree.
10
/ \
20 30 */
void inorder_array(struct Node*,int [],int *) ;
bool isBST(struct Node* root) {
int size=0,a[100],i ;
if(root==NULL)
return true ;
inorder_array(root,a,&size) ;
for(i=0;i<size-1;i++)
{
if(a[i]>a[i+1])
return false ;
}
return true ;
}
void inorder_array(struct Node* root,int a[],int *s)
{
if(root==NULL)
return ;
inorder_array(root->left,a,&(*s)) ;
a[(*s)++]=root->data ;
inorder_array(root->right,a,&(*s)) ;
}
---------------------------------------------------------------------------------
Given a binary tree, return true if it is BST, else false. For example, the following tree is not BST, because 11 is in left subtree of 10.
10
/ \
7 39
\
11
Input:
The task is to complete the method which takes one argument, root of Binary Tree. The struct Node has a data part which stores the data, pointer to left child and pointer to right child.
There are multiple test cases. For each test case, this method will be called individually.
Output:
The function should return count of leaves
Constraints:
1 <=T<= 30
1 <= Number of nodes<= 100
1 <= Data of a node<= 1000
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* A binary tree node has data, pointer to left child
and a pointer to right child
struct Node {
int data;
struct Node* left, * right;
}; */
/* Should return true if tree represented by root is BST.
For example, return value should be true for following tree.
20
/ \
10 30
and return value should be false for following tree.
10
/ \
20 30 */
void inorder_array(struct Node*,int [],int *) ;
bool isBST(struct Node* root) {
int size=0,a[100],i ;
if(root==NULL)
return true ;
inorder_array(root,a,&size) ;
for(i=0;i<size-1;i++)
{
if(a[i]>a[i+1])
return false ;
}
return true ;
}
void inorder_array(struct Node* root,int a[],int *s)
{
if(root==NULL)
return ;
inorder_array(root->left,a,&(*s)) ;
a[(*s)++]=root->data ;
inorder_array(root->right,a,&(*s)) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment