-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path98.cpp
More file actions
22 lines (22 loc) · 752 Bytes
/
Copy path98.cpp
File metadata and controls
22 lines (22 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
bool isValidBST(TreeNode *root) {
if (!root)
return true;
stack<tuple<TreeNode *, int *, int *>> s;
s.push(make_tuple(root, nullptr, nullptr));
while (!s.empty()) {
auto curr = s.top();
s.pop();
auto node = get<0>(curr);
auto &min_val = get<1>(curr), &max_val = get<2>(curr);
if ((min_val && *min_val >= node->val) || (max_val && *max_val <= node->val))
return false;
if (node->left)
s.push(make_tuple(node->left, min_val, &node->val));
if (node->right)
s.push(make_tuple(node->right, &node->val, max_val));
}
return true;
}
};