blob: 73cefa0afb126c960d3c49f1bcd7e7acea87858a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#include <cstddef>
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
#include <limits>
class Solution
{
public:
bool dfs(TreeNode *node, long long min, long long max)
{
if (node == nullptr)
return true;
if (node->val <= min || node->val >= max)
return false;
return dfs(node->left, min, node->val) && dfs(node->right, node->val, max);
}
bool isValidBST(TreeNode *root)
{
return dfs(root, std::numeric_limits<long long>::min(), std::numeric_limits<long long>::max());
}
};
|