"check for bst" Code Answer's
You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "check for bst" answered properly. Developers are finding an appropriate answer about check for bst related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like check for bst. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on check for bst.
check for bst
// C++ program to check if a given tree is BST.
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data, pointer to
left child and a pointer to right child */
struct Node
{
int data;
struct Node* left, *right;
};
// Returns true if given tree is BST.
bool isBST(Node* root, Node* l=NULL, Node* r=NULL)
{
// Base condition
if (root == NULL)
return true;
// if left node exist then check it has
// correct data or not i.e. left node's data
// should be less than root's data
if (l != NULL and root->data <= l->data)
return false;
// if right node exist then check it has
// correct data or not i.e. right node's data
// should be greater than root's data
if (r != NULL and root->data >= r->data)
return false;
// check recursively for every node.
return isBST(root->left, l, root) and
isBST(root->right, root, r);
}
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct Node* newNode(int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
/* Driver program to test above functions*/
int main()
{
struct Node *root = newNode(3);
root->left = newNode(2);
root->right = newNode(5);
root->left->left = newNode(1);
root->left->right = newNode(4);
if (isBST(root,NULL,NULL))
cout << "Is BST";
else
cout << "Not a BST";
return 0;
}
All those coders who are working on the C++ based application and are stuck on check for bst can get a collection of related answers to their query. Programmers need to enter their query on check for bst related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about check for bst for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of check for bst while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "check for bst". Visit this developer's friendly online web community, CodeProZone, and get your queries like check for bst resolved professionally and stay updated to the latest C++ updates.