LeetCode 98. 验证二叉搜索树
原题链接
中等
作者:
toFuture
,
2025-03-29 00:43:21
· 江苏
,
所有人可见
,
阅读 1
/**
* 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) {
if(!root)return true;
return dfs(root)[0];
}
vector<int> dfs(TreeNode* root)
{
vector<int> res({1, root->val, root->val});
if(root->left)
{
auto t=dfs(root->left);
if(!t[0]||t[2]>=root->val)res[0]=0;
res[1]=min(res[1], t[1]);
res[2]=max(res[2], t[2]);
}
if(root->right)
{
auto t=dfs(root->right);
if(!t[0]||t[2]<=root->val)res[0]=0;
res[1]=min(res[1], t[1]);
res[2]=max(res[2], t[2]);
}
return res;
}
};