Skip to content

Commit 1ae2729

Browse files
committed
Fix: validate bin search tree
1 parent 59809f9 commit 1ae2729

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

validate-binary-search-tree/dylan-jung.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
1010
* };
1111
*/
12-
class Solution {
12+
class Solution {
1313
public:
1414
bool dfs(TreeNode* root, long minVal, long maxVal) {
1515
if(!(minVal < root->val && root->val < maxVal)) return false;
1616
bool isValid = true;
17-
if(root->left) {
18-
isValid = isValid && dfs(root->left, minVal, min((long)root->val, maxVal));
17+
if (root->left) {
18+
isValid = isValid && dfs(root->left, minVal, root->val);
1919
}
20-
if(root->right) {
21-
isValid = isValid && dfs(root->right, max((long)root->val, minVal), maxVal);
20+
if (root->right) {
21+
isValid = isValid && dfs(root->right, root->val, maxVal);
2222
}
2323
return isValid;
2424
}
2525

2626
bool isValidBST(TreeNode* root) {
27+
if(!root) return true;
2728
return dfs(root, -(1l << 32), 1l << 32);
2829
}
2930
};

0 commit comments

Comments
 (0)