Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Hidden stub code will pass a root argument to the function below. Complete the function to solve the challenge. Hint: you may want to write one or more helper functions.
- The Node struct is defined as follows:
- struct Node {
- int data;
- Node* left;
- Node* right;
- }
- */
- #include <limits.h>
- int find_max(Node* root){
- if (!root) return INT_MIN;
- int max_right = find_max(root->right);
- int max_left = find_max(root->left);
- return max(max(max_right, max_left), root->data);
- }
- int find_min(Node* root){
- if (!root) return INT_MAX;
- int min_right = find_min(root->right);
- int min_left = find_min(root->left);
- return min(min(min_right, min_left), root->data);
- }
- bool checkBST(Node* root) {
- if (!root) return true;
- if(root->left && find_max(root->left) >= root->data)
- return false;
- if(root->right && find_min(root->right) <= root->data)
- return false;
- return checkBST(root->right) && checkBST(root->left);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement