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
Output of Node Count in Simple Binary Tree
What is the output of the following TypeScript code that counts total nodes in a binary tree?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) { this.val = val; this.left = left; this.right = right; } } function countNodes(root: TreeNode | null): number { if (root === null) return 0; return 1 + countNodes(root.left) + countNodes(root.right); } const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3)); console.log(countNodes(tree));
Attempts:
2 left
💡 Hint
Count the root node plus nodes in left and right subtrees.
✗ Incorrect
The tree has root node 1, left child 2, and right child 3, total 3 nodes.
❓ Predict Output
intermediate2:00remaining
Count Nodes in Unbalanced Binary Tree
What will be printed after running this code that counts nodes in an unbalanced binary tree?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) { this.val = val; this.left = left; this.right = right; } } function countNodes(root: TreeNode | null): number { if (root === null) return 0; return 1 + countNodes(root.left) + countNodes(root.right); } const tree = new TreeNode(10, new TreeNode(5, new TreeNode(3)), null); console.log(countNodes(tree));
Attempts:
2 left
💡 Hint
Count all nodes including left subtree nodes.
✗ Incorrect
The tree has nodes 10, 5, and 3, total 3 nodes.
🔧 Debug
advanced2:00remaining
Identify the Error in Node Counting Function
What error will this TypeScript code produce when counting nodes in a binary tree?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) { this.val = val; this.left = left; this.right = right; } } function countNodes(root: TreeNode | null): number { if (root === null) return 0; return 1 + countNodes(root.left) - countNodes(root.right); } const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3)); console.log(countNodes(tree));
Attempts:
2 left
💡 Hint
Check the operator used between recursive calls.
✗ Incorrect
The code subtracts right subtree count instead of adding: 1 + 1 - 1 = 1 (logical error, outputs 1 instead of 3).
❓ Predict Output
advanced2:00remaining
Count Nodes in Large Balanced Binary Tree
What is the output of this code counting nodes in a balanced binary tree of height 3?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) { this.val = val; this.left = left; this.right = right; } } function countNodes(root: TreeNode | null): number { if (root === null) return 0; return 1 + countNodes(root.left) + countNodes(root.right); } const tree = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, new TreeNode(6), new TreeNode(7)) ); console.log(countNodes(tree));
Attempts:
2 left
💡 Hint
Count all nodes in a full binary tree with 3 levels.
✗ Incorrect
A full binary tree with height 3 has 7 nodes total.
🧠 Conceptual
expert2:00remaining
Time Complexity of Counting Nodes in Binary Tree
What is the time complexity of the recursive function that counts total nodes in a binary tree with n nodes?
Attempts:
2 left
💡 Hint
Consider how many nodes the function visits.
✗ Incorrect
The function visits each node once, so time complexity is linear O(n).