Given a binary tree,write an algorithm to find if the tree is a binary search tree or not. Simple Recursive call is good.. int check_tree(bst *node) { if(!node) return TRUE; if(node->left!=NULL && node->info left)) return(false); if(node->right!=NULL && node->info right)) return(false); if(!check_tree(node->left) || !check_tree(node->right)) return(false); return(true); } I have already posted better algorithm in my tree sections earlier. Set Min and Max to INT_MIN and INT_MAX bool IsBst( node *root, int min, int max) { if (!root) return true; if ( !isBst( root->left,min,root->info) return false; if ( !isBst(root->right,root->info,max) return false; return true; }
Collection of Interview Question on Data structure, Algorithm, Java, C++ and more