/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: bool isValidBST(TreeNode* root) { return isBST(root,LONG_MIN, LONG_MAX); } bool isBST(TreeNode* root ,long long min,long long max) { if(!root) { return true; } if(root->val<=min || root->val>=max) //注意边界!! 不能存在等值 { return false; } return isBST(root->left,min,root->val) && isBST(root->right,root->val,max); } };
每天进步一点点~