Challenge - 5 Problems
Binary Tree Node Counting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of counting nodes in this binary tree?
Given the following Java code that counts total nodes in a binary tree, what will be the output printed?
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class BinaryTree { Node root; int countNodes(Node node) { if (node == null) return 0; return 1 + countNodes(node.left) + countNodes(node.right); } public static void main(String[] args) { BinaryTree 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); System.out.println(tree.countNodes(tree.root)); } }
Attempts:
2 left
💡 Hint
Count each node starting from root, including all children.
✗ Incorrect
The tree has nodes: 10 (root), 20 (left child), 30 (right child), 40 (left-left child), 50 (left-right child). Total nodes = 5.
❓ Predict Output
intermediateWhat is the output after adding one more node?
If we add one more node as the right child of node 30 in the previous tree, what will be the output of countNodes?
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class BinaryTree { Node root; int countNodes(Node node) { if (node == null) return 0; return 1 + countNodes(node.left) + countNodes(node.right); } public static void main(String[] args) { BinaryTree 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.right = new Node(60); System.out.println(tree.countNodes(tree.root)); } }
Attempts:
2 left
💡 Hint
Count all nodes including the new one added.
✗ Incorrect
The new node 60 is added as right child of 30, increasing total nodes to 6.
🧠 Conceptual
advancedWhy does the countNodes method use recursion?
Why is recursion a good choice to count total nodes in a binary tree?
Attempts:
2 left
💡 Hint
Think about how trees have branches and recursion follows branches easily.
✗ Incorrect
Recursion fits tree structures well because it processes each node and its children naturally, making code simple and clear.
🔧 Debug
advancedWhat error occurs in this code?
What error will this code produce when counting nodes?
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class BinaryTree { Node root; int countNodes(Node node) { if (node == null) return 0; return 1 + countNodes(node.left) + countNodes(node.right); } public static void main(String[] args) { BinaryTree 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.left.left.left = tree.root; // creates cycle System.out.println(tree.countNodes(tree.root)); } }
Attempts:
2 left
💡 Hint
Think about what happens if a node points back to an ancestor.
✗ Incorrect
Assigning root as left child of a descendant creates a cycle causing infinite recursion and stack overflow.
🚀 Application
expertHow to modify countNodes to avoid infinite recursion in cyclic trees?
Given a binary tree that may contain cycles, how can you modify the countNodes method to avoid infinite recursion and count each node only once?
Attempts:
2 left
💡 Hint
Think about remembering which nodes you have already counted.
✗ Incorrect
Tracking visited nodes with a Set prevents revisiting nodes in cycles, avoiding infinite recursion and counting nodes once.
