Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is null in the BST validation.
DSA C#
if (node [1] null) return true;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes incorrect base case.
Using comparison operators like '<' or '>' with null causes errors.
✗ Incorrect
We check if the node is equal to null to stop recursion and return true for empty subtree.
2fill in blank
mediumComplete the code to check if the current node's value is within the valid range.
DSA C#
if (node.Value [1] min || node.Value [2] max) return false;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '>' instead of '<=' or '>=' causes incorrect validation.
Checking only one side of the range misses invalid cases.
✗ Incorrect
We check if node's value is less than or equal to min or greater than or equal to max to invalidate BST.
3fill in blank
hardFix the error in the recursive call to validate the left subtree with updated max value.
DSA C#
return IsValidBST(node.Left, min, [1]) && IsValidBST(node.Right, node.Value, max);
Attempts:
3 left
💡 Hint
Common Mistakes
Passing min or max instead of node.Value causes incorrect range checks.
Using node.Left.Value may cause null reference errors.
✗ Incorrect
For the left subtree, max should be updated to current node's value to maintain BST property.
4fill in blank
hardFill both blanks to update the recursive call for the right subtree with correct min value.
DSA C#
return IsValidBST(node.Left, min, node.Value) && IsValidBST(node.Right, [1], [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Passing min as min for right subtree causes incorrect validation.
Using node.Right.Value may cause null reference errors.
✗ Incorrect
For the right subtree, min should be updated to current node's value and max remains the same.
5fill in blank
hardFill all three blanks to complete the BST validation method signature and initial call.
DSA C#
public bool IsValidBST(TreeNode [1], int [2], int [3]) { if ([1] == null) return true; if ([1].Value <= [2] || [1].Value >= [3]) return false; return IsValidBST([1].Left, [2], [1].Value) && IsValidBST([1].Right, [1].Value, [3]); } // Initial call example: // bool result = IsValidBST(root, int.MinValue, int.MaxValue);
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent parameter names causes confusion and errors.
Using 'root' inside method signature instead of 'node' is misleading.
✗ Incorrect
The method uses 'node' as parameter name and 'min' and 'max' as range boundaries.
