Advertisement
CSenshi

isBST?

Jun 21st, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. /* 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.  
  2.  
  3. The Node struct is defined as follows:
  4.    struct Node {
  5.       int data;
  6.       Node* left;
  7.       Node* right;
  8.    }
  9. */
  10.  
  11.  
  12. bool left(Node *root , int n){
  13.     if(root == NULL){
  14.         return 1;
  15.     }else if(root->data >= n){
  16.         return 0;
  17.     }else{
  18.         return left(root->left , root->data)
  19.             && left(root->left , n)
  20.             && left(root->right , n)
  21.             && right(root->right, root->data);
  22.     }
  23. }
  24.  
  25. bool right (Node *root , int n){
  26.     if(root == NULL){
  27.         return 1;
  28.     }else if(root->data <= n){
  29.         return 0;
  30.     }else{
  31.         return right(root->right , root->data)
  32.             && right(root->right, n)
  33.             && right(root->left , n)
  34.             && left(root->left , root->data);
  35.     }
  36. }
  37.  
  38. bool checkBST(Node * root) {
  39.    if(root == NULL) return true;
  40.     return right(root->right , root->data) && left(root->left , root->data);
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement