Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the total number of nodes in the binary tree.
DSA Javascript
function countNodes(root) {
if (root === null) return [1];
return 1 + countNodes(root.left) + countNodes(root.right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 for null nodes.
Returning null which causes errors in addition.
✗ Incorrect
The base case returns 0 when the node is null, meaning no nodes here.
2fill in blank
mediumComplete the code to check if the current node is null.
DSA Javascript
function countNodes(root) {
if (root [1] null) return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which allows type coercion.
Using != or !== which check inequality.
✗ Incorrect
Use strict equality (===) to check if root is exactly null.
3fill in blank
hardFix the error in the recursive call to count nodes in the left subtree.
DSA Javascript
function countNodes(root) {
if (root === null) return 0;
return 1 + countNodes(root.[1]) + countNodes(root.right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Typo in 'left' as 'leftt'.
Using 'right' instead of 'left'.
✗ Incorrect
The left subtree is accessed by root.left, not root.leftt or others.
4fill in blank
hardFill both blanks to complete the function that counts nodes recursively.
DSA Javascript
function countNodes(root) {
if (root [1] null) return [2];
return 1 + countNodes(root.left) + countNodes(root.right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using loose equality (==) instead of strict (===).
Returning 1 instead of 0 for null nodes.
✗ Incorrect
Use strict equality to check null and return 0 for no nodes.
5fill in blank
hardFill all three blanks to create a concise recursive count of nodes.
DSA Javascript
const countNodes = (root) => {
if (root [1] null) return [2];
return 1 + countNodes(root.[3]) + countNodes(root.right);
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using !== instead of === for null check.
Returning 1 instead of 0 for null.
Typo in 'left' property.
✗ Incorrect
Check if root is null with ===, return 0, and recurse on root.left.