nikunjsoni

98

May 10th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     bool isValidBST(TreeNode* root) {
  15.         if(!root) return true;
  16.         return isValid(root, LONG_LONG_MIN, LONG_LONG_MAX);
  17.     }
  18.    
  19.     bool isValid(TreeNode *root, long long min, long long max){
  20.         if(!root) return true;
  21.         if(root->val <= min || root->val >= max) return false;
  22.         return (isValid(root->left, min, root->val) && isValid(root->right, root->val, max));
  23.     }
  24.    
  25. };
Advertisement
Add Comment
Please, Sign In to add comment