You are viewing a single comment's thread. Return to all comments →
Just a little improvement. As soon as flag is false we don't need to follow with the right tree.
Node* prevNode = NULL; bool inOrderTraversal(Node *node, bool flag) { if(node->left != NULL) { flag = inOrderTraversal(node->left, flag); } if(prevNode != NULL && prevNode->data >= node->data) { flag = false; } else { prevNode = node; if(node->right != NULL) { flag = inOrderTraversal(node->right, flag); } } return flag; } bool checkBST(Node *root) { bool flag = true; return inOrderTraversal(root,flag); }
With this little improvement pass all test cases as well
Regards
Seems like cookies are disabled on this browser, please enable them to open this website
Trees: Is This a Binary Search Tree?
You are viewing a single comment's thread. Return to all comments →
Just a little improvement. As soon as flag is false we don't need to follow with the right tree.
With this little improvement pass all test cases as well
Regards