Challenge - 5 Problems
Binary Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Manual Binary Tree Creation
What is the printed output of the binary tree after manually creating nodes and linking them as shown in the code?
DSA Java
class Node { int value; Node left, right; Node(int value) { this.value = value; left = right = null; } } public class BinaryTree { public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); root.right.right = new Node(18); printInOrder(root); } static void printInOrder(Node node) { if (node == null) return; printInOrder(node.left); System.out.print(node.value + " -> "); printInOrder(node.right); } }
Attempts:
2 left
💡 Hint
In-order traversal prints left subtree, then node, then right subtree.
✗ Incorrect
The code performs an in-order traversal of the binary tree, which visits nodes in ascending order for a binary search tree. The nodes are printed as 3, 5, 7, 10, 15, 18 with arrows.
❓ Predict Output
intermediateOutput after Adding a New Node
After adding a new node with value 6 as the left child of node 7, what is the in-order traversal output?
DSA Java
class Node { int value; Node left, right; Node(int value) { this.value = value; left = right = null; } } public class BinaryTree { public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); root.left.right.left = new Node(6); root.right.right = new Node(18); printInOrder(root); } static void printInOrder(Node node) { if (node == null) return; printInOrder(node.left); System.out.print(node.value + " -> "); printInOrder(node.right); } }
Attempts:
2 left
💡 Hint
In-order traversal visits left subtree, node, then right subtree recursively.
✗ Incorrect
The new node 6 is the left child of 7, so it appears between 5 and 7 in in-order traversal.
🔧 Debug
advancedIdentify the Error in Binary Tree Node Linking
What error will occur when running this code that manually creates a binary tree?
DSA Java
class Node { int value; Node left, right; Node(int value) { this.value = value; left = right = null; } } public class BinaryTree { public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); root.right.left.right = new Node(18); printInOrder(root); } static void printInOrder(Node node) { if (node == null) return; printInOrder(node.left); System.out.print(node.value + " -> "); printInOrder(node.right); } }
Attempts:
2 left
💡 Hint
Check if all intermediate nodes are initialized before accessing their children.
✗ Incorrect
root.right.left is null by default, so accessing root.right.left.right causes NullPointerException.
🧠 Conceptual
advancedNumber of Nodes in a Manually Created Binary Tree
Given the following manual binary tree creation code, how many nodes does the tree contain?
DSA Java
Node 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); root.left.left.left = new Node(8);
Attempts:
2 left
💡 Hint
Count each new Node() call in the code.
✗ Incorrect
There are 8 Node objects created and linked, so the tree has 8 nodes.
🚀 Application
expertManual Binary Tree Height Calculation
What is the height of the binary tree created by the following code? Height is the number of edges on the longest path from root to a leaf.
DSA Java
Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.left.left = new Node(1); root.right.right = new Node(18); root.right.right.right = new Node(20);
Attempts:
2 left
💡 Hint
Count edges from root to the deepest leaf node.
✗ Incorrect
The longest path from root to a leaf has 3 edges, for example, root(10) -> 5 -> 3 -> 1 or 10 -> 15 -> 18 -> 20. Thus, the height is 3.
