Challenge - 5 Problems
Binary Tree Node Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Simple Binary Tree Node Creation
What is the printed output after creating a binary tree node with value 10 and printing its value and children?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; this.left = null; this.right = null; } } public class Main { public static void main(String[] args) { Node root = new Node(10); System.out.print(root.val + " -> "); System.out.print(root.left + " -> "); System.out.print(root.right); } }
Attempts:
2 left
💡 Hint
Remember that uninitialized object references in Java default to null.
✗ Incorrect
The node is created with value 10 and both left and right children are null. Printing null references shows 'null'.
❓ Predict Output
intermediateOutput After Linking Left Child Node
What is the output after creating a root node with value 5 and linking a left child node with value 3, then printing the values?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; this.left = null; this.right = null; } } public class Main { public static void main(String[] args) { Node root = new Node(5); root.left = new Node(3); System.out.print(root.val + " -> "); System.out.print(root.left.val + " -> "); System.out.print(root.right); } }
Attempts:
2 left
💡 Hint
Check what happens when you print a null reference versus an object.
✗ Incorrect
The root has value 5, left child has value 3, right child is null. Printing left.val prints 3, right prints null.
🔧 Debug
advancedIdentify the Error in Binary Tree Node Initialization
What error will this code produce when trying to compile and run?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class Main { public static void main(String[] args) { Node root = new Node(1); System.out.println(root.val); } }
Attempts:
2 left
💡 Hint
Check the line where left and right are assigned.
✗ Incorrect
The line 'left = right = null' is missing a semicolon at the end, causing a syntax error.
🧠 Conceptual
advancedUnderstanding Null Children in Binary Tree Nodes
Why do we initialize left and right child nodes to null in a binary tree node?
Attempts:
2 left
💡 Hint
Think about what null means for references in Java.
✗ Incorrect
Null means the node does not point to any child node yet, showing the tree structure clearly.
🚀 Application
expertResulting Tree Structure After Multiple Insertions
Given the following code that creates nodes and links them, what is the printed output representing the tree nodes in preorder (root -> left -> right)?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; this.left = null; this.right = null; } } public class Main { public static void preorder(Node node) { if (node == null) return; System.out.print(node.val + " -> "); preorder(node.left); preorder(node.right); } 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); preorder(root); } }
Attempts:
2 left
💡 Hint
Preorder traversal visits root first, then left subtree, then right subtree.
✗ Incorrect
The traversal order is root(10), left(5), left-left(3), left-right(7), right(15).
