class Solution {
public:
int pre = INT_MIN;
int flag = 0;
bool isValidBST(TreeNode* root) {
return inOrderTraversal(root);
}
bool inOrderTraversal(TreeNode* root)
{
if (root == NULL)
{
return true;
}
bool left = inOrderTraversal(root->left);
if (!flag)
{
flag = 1;
}
else
{
if (root->val <= pre)
{
return false;
}
}
pre = root->val;
bool right = inOrderTraversal(root->right);
return left && right;
}
};
LeetCode面试题 04.05. 合法二叉搜索树
最新推荐文章于 2023-03-08 10:00:00 发布