typedef long long ll;
class Solution {
public:
bool dfs(TreeNode* cur, ll l, ll r) {
if (!cur) return true;
if (cur -> val <= l || r <= cur -> val) return false;
return dfs(cur -> left, l, cur -> val) && dfs(cur -> right, cur -> val, r);
}
bool isValidBST(TreeNode* root) {
return dfs(root, LLONG_MIN, LLONG_MAX);
}
};
Time Complexity: O(n)
Space: O(1)
也是一題很好的 DFS 基礎題目