Challenge - 5 Problems
Binary Tree Node Counting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateCount nodes in a simple binary tree
What is the output of the following code that counts total nodes in a binary tree?
DSA C#
public class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } public class BinaryTree { public Node Root; public int CountNodes(Node node) { if (node == null) return 0; return 1 + CountNodes(node.Left) + CountNodes(node.Right); } } var tree = new BinaryTree(); tree.Root = new Node(1); tree.Root.Left = new Node(2); tree.Root.Right = new Node(3); tree.Root.Left.Left = new Node(4); int total = tree.CountNodes(tree.Root); System.Console.WriteLine(total);
Attempts:
2 left
💡 Hint
Count each node including root and all children recursively.
✗ Incorrect
The tree has nodes with values 1, 2, 3, and 4. Total nodes are 4.
❓ Predict Output
intermediateCount nodes in a full binary tree
What will be printed after counting nodes in this full binary tree?
DSA C#
public class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } public class BinaryTree { public Node Root; public int CountNodes(Node node) { if (node == null) return 0; return 1 + CountNodes(node.Left) + CountNodes(node.Right); } } var tree = new BinaryTree(); tree.Root = new Node(10); tree.Root.Left = new Node(20); tree.Root.Right = new Node(30); tree.Root.Left.Left = new Node(40); tree.Root.Left.Right = new Node(50); tree.Root.Right.Left = new Node(60); tree.Root.Right.Right = new Node(70); int total = tree.CountNodes(tree.Root); System.Console.WriteLine(total);
Attempts:
2 left
💡 Hint
Count all nodes including root and all children.
✗ Incorrect
The tree has 7 nodes: 10,20,30,40,50,60,70.
🧠 Conceptual
advancedUnderstanding node counting complexity
What is the time complexity of counting total nodes in a binary tree using recursion?
Attempts:
2 left
💡 Hint
Each node is visited once in the recursive count.
✗ Incorrect
The recursive function visits each node exactly once, so time complexity is linear in number of nodes.
🔧 Debug
advancedIdentify the bug in node counting code
What error will this code produce when counting nodes in a binary tree?
DSA C#
public class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } public class BinaryTree { public Node Root; public int CountNodes(Node node) { if (node == null) return 0; return CountNodes(node.Left) + CountNodes(node.Right); } } var tree = new BinaryTree(); tree.Root = new Node(1); tree.Root.Left = new Node(2); int total = tree.CountNodes(tree.Root); System.Console.WriteLine(total);
Attempts:
2 left
💡 Hint
Check if the current node is counted in the return statement.
✗ Incorrect
The code misses counting the current node (no +1), so it outputs 0 instead of 2.
🚀 Application
expertCount nodes in a binary tree with null nodes explicitly stored
Given a binary tree where null children are stored as explicit nodes with Value = -1, how many nodes will the count method return?
DSA C#
public class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } public class BinaryTree { public Node Root; public int CountNodes(Node node) { if (node == null) return 0; return 1 + CountNodes(node.Left) + CountNodes(node.Right); } } var tree = new BinaryTree(); tree.Root = new Node(1); tree.Root.Left = new Node(-1); // explicit null // Right child is null reference int total = tree.CountNodes(tree.Root); System.Console.WriteLine(total);
Attempts:
2 left
💡 Hint
Count all nodes including explicit null nodes.
✗ Incorrect
The tree has root node (1) and one explicit null node (-1). Right child is null reference and not counted.
