Challenge - 5 Problems
Binary Tree Node Counting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Count nodes in a simple binary tree
What is the output of the following code that counts total nodes in a binary tree?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function countNodes(root) { if (!root) return 0; return 1 + countNodes(root.left) + countNodes(root.right); } const root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); console.log(countNodes(root));
Attempts:
2 left
💡 Hint
Count each node including root, left child, right child, and left-left child.
✗ Incorrect
The tree has 4 nodes: root (10), left (5), right (15), and left-left (3). The function counts all nodes recursively.
❓ Predict Output
intermediate1:00remaining
Count nodes in an empty tree
What is the output when counting nodes in an empty binary tree?
DSA Javascript
function countNodes(root) {
if (!root) return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
}
const root = null;
console.log(countNodes(root));Attempts:
2 left
💡 Hint
An empty tree has no nodes.
✗ Incorrect
The function returns 0 immediately when root is null, meaning no nodes exist.
❓ Predict Output
advanced2:00remaining
Count nodes in a full binary tree
What is the output of counting nodes in this full binary tree?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function countNodes(root) { if (!root) return 0; return 1 + countNodes(root.left) + countNodes(root.right); } const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.left = new Node(6); root.right.right = new Node(7); console.log(countNodes(root));
Attempts:
2 left
💡 Hint
Count all nodes including root, its two children, and their children.
✗ Incorrect
The tree has 7 nodes: root (1), two children (2,3), and four grandchildren (4,5,6,7).
❓ Predict Output
advanced2:00remaining
Count nodes in an unbalanced binary tree
What is the output of counting nodes in this unbalanced binary tree?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function countNodes(root) { if (!root) return 0; return 1 + countNodes(root.left) + countNodes(root.right); } const root = new Node(10); root.left = new Node(5); root.left.left = new Node(3); root.left.left.left = new Node(1); root.right = new Node(15); console.log(countNodes(root));
Attempts:
2 left
💡 Hint
Count all nodes including the deep left branch.
✗ Incorrect
The tree has 5 nodes: 10, 5, 3, 1 on the left side and 15 on the right.
🧠 Conceptual
expert1:30remaining
Time complexity of counting nodes in binary tree
What is the time complexity of counting total nodes in a binary tree using a recursive function that visits each node once?
Attempts:
2 left
💡 Hint
The function visits every node exactly once.
✗ Incorrect
Since the function visits each node once, the time grows linearly with the number of nodes.