leetcode 98. 验证二叉搜索树
题目如下
数据范围
通过代码
/**
* 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:
long long t = INT_MIN;
bool ans = true;
void in(TreeNode *root){
if(root == nullptr || !ans)return;
in(root->left);
if(root->val <= t){
ans = false;
return;
}
t = root->val;
in(root->right);
}
bool isValidBST(TreeNode* root) {
t = t * 2;
in(root);
return ans;
}
};