You are viewing a single comment's thread. Return to all comments →
As a simple solution without much complications I did inorder traversal of the tree and checked if the elements are in sorted order.
void inorder(Node *root,vector<int> &a){ if(root){ inorder(root ->left,a); a.push_back(root -> data); inorder(root ->right,a); } } bool checkBST(Node* root) { vector<int> a; inorder(root,a); bool found = true; for(int i = 0 ; i < a.size() - 1; i++ ){ if(a.at(i) >= a.at(i+1)){ found = false; break; } } return found; }
Seems like cookies are disabled on this browser, please enable them to open this website
Is This a Binary Search Tree?
You are viewing a single comment's thread. Return to all comments →
As a simple solution without much complications I did inorder traversal of the tree and checked if the elements are in sorted order.