leetcode updates the test set of this problem.
so that we must change our solution and we cannot JUST use the INT_MAX and the INT_MIN at all.
I use mid-order to determine whether the array is in the increasing sequence. And consider the first element individually.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int last = INT_MIN;
bool first = true;
bool dfs(TreeNode* root)
{
if(root == NULL) return true;
if(root ->left)
if(!dfs(root->left))
return false;
if(!first)
{
if(root ->val <= last)
return false;
else
last = root->val;
}
else
{
last = root->val;
first = false;
}
if(root->right)
if(!dfs(root->right))
return false;
return true;
}
bool isValidBST(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return dfs(root);
}
};