Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is null.
DSA Typescript
if (root [1] null) return true;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '===' causes incorrect null check.
Using '>' or '<' operators for null comparison.
✗ Incorrect
We check if root is exactly null to handle the base case of recursion.
2fill in blank
mediumComplete the code to check if the current node's value is within the valid range.
DSA Typescript
if (root.val [1] min || root.val [2] max) return false;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '>' instead of '<=' or '>=' causes boundary errors.
Checking only one side of the range.
✗ Incorrect
The node's value must be strictly greater than min and strictly less than max, so we check if it is less than or equal to min or greater than or equal to max to return false.
3fill in blank
hardFix the error in the recursive call to validate left subtree with updated max.
DSA Typescript
return isValidBST(root.left, min, [1]) && isValidBST(root.right, root.val, max);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing min or max incorrectly causes wrong validation.
Using root.left.val instead of root.val.
✗ Incorrect
For the left subtree, the maximum allowed value is the current node's value.
4fill in blank
hardFill both blanks to correctly validate the right subtree with updated min.
DSA Typescript
return isValidBST(root.left, min, root.val) && isValidBST(root.right, [1], [2]);
Drag options to blanks, or click blank then click option'
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, and max remains unchanged.
5fill in blank
hardFill all three blanks to complete the function signature and initial call for BST validation.
DSA Typescript
function isValidBST(root: TreeNode | null, [1]: number = -Infinity, [2]: number = [3]): boolean { if (root === null) return true; if (root.val <= min || root.val >= max) return false; return isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting default values causes errors.
Using wrong parameter names or default values.
✗ Incorrect
The function uses min and max parameters with default values -Infinity and Infinity to check valid ranges.