Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool is_leaf(TreeNode* root) {
- if(root == NULL) return 0;
- return (root->left == NULL) and (root->right == NULL);
- }
- // We are considering that BSTs can not contain duplicate Nodes.
- bool check_bst(TreeNode *root) {
- if((root == NULL) or (is_leaf(root) == 1)) return 1;
- bool res = 1;
- if(root->left) res &= (root->left->val < root->val);
- if(root->right) res &= (root->right->val > root->val);
- return (res and check_bst(root->left) and check_bst(root->right));
- }
- /* One possible tree where the above algorithm will give wrong result is :--->
- 5
- / \
- / \
- 4 6
- / \
- / \
- 3 7
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement