Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a node is null.
DSA Javascript
if (node [1] null) return true;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '===' which can cause unexpected type coercion.
Using '!=' or '!==' incorrectly.
✗ Incorrect
We check if node is exactly equal to null to handle empty nodes.
2fill in blank
mediumComplete the code to calculate the height of a node recursively.
DSA Javascript
function height(node) {
if (node === null) return 0;
return 1 + Math.max(height(node.left), [1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling height on node.left twice instead of node.right.
Calling height on node or node.parent which causes infinite recursion or wrong result.
✗ Incorrect
Height is 1 plus the maximum height of left and right subtrees.
3fill in blank
hardFix the error in the balance check condition.
DSA Javascript
if (Math.abs(height(node.left) - height(node.right)) [1] 1) return false;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' which incorrectly returns false for balanced nodes.
Using '===' which only checks exact difference of 1.
✗ Incorrect
A binary tree is balanced if the height difference is not more than 1, so return false if difference is greater than 1.
4fill in blank
hardFill both blanks to complete the recursive balanced tree check.
DSA Javascript
return isBalanced(node.[1]) && isBalanced(node.[2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' or 'child' which are not valid properties for this check.
✗ Incorrect
We check if both left and right subtrees are balanced recursively.
5fill in blank
hardFill all three blanks to complete the full isBalanced function.
DSA Javascript
function isBalanced(node) {
if (node [1] null) return true;
if (Math.abs(height(node.left) - height(node.right)) [2] 1) return false;
return isBalanced(node.[3]) && isBalanced(node.right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' in height difference check.
Using 'parent' or other invalid properties.
✗ Incorrect
Check if node is null, then if height difference is greater than 1, then recursively check left and right subtrees.