Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is null.
DSA Java
if (root [1] null) { return true; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong base case.
Using comparison operators like '>' or '<' with null causes errors.
✗ Incorrect
We check if the current node is null using '==' to return true for an empty subtree.
2fill in blank
mediumComplete the code to check if the current node's value is within the valid range.
DSA Java
if (root.val [1] min || root.val [2] max) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '>' instead of '<=' or '>=' causes incorrect validation.
Mixing up min and max comparisons.
✗ Incorrect
The node's value must be strictly greater than min and less than max, so we check if it is less or equal to min or greater or equal to max to return false.
3fill in blank
hardFix the error in the recursive call to validate left subtree with updated max value.
DSA Java
return isValidBST(root.left, min, [1]) && isValidBST(root.right, root.val, max);
Attempts:
3 left
💡 Hint
Common Mistakes
Passing min or max incorrectly causes wrong validation.
Using root.left.val instead of root.val causes errors.
✗ Incorrect
For the left subtree, the maximum allowed value is the current node's value, so we pass root.val as max.
4fill in blank
hardFill both blanks to update the recursive call for the right subtree with the new min value.
DSA Java
return isValidBST(root.left, min, root.val) && isValidBST(root.right, [1], [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping min and max values.
Using root.right.val instead of root.val.
✗ Incorrect
For the right subtree, the minimum allowed value is the current node's value (root.val), and the maximum remains max.
5fill in blank
hardFill all three blanks to complete the method signature and initial call for BST validation.
DSA Java
public boolean isValidBST([1] root) { return isValidBST(root, [2], [3]); } private boolean isValidBST(TreeNode root, Integer min, Integer max) { if (root == null) return true; if ((min != null && root.val <= min) || (max != null && root.val >= max)) return false; return isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using primitive int instead of Integer causes null issues.
Passing 0 instead of null for min or max.
✗ Incorrect
The public method takes a TreeNode root and calls the private helper with null min and max to start without boundaries.
