Challenge - 5 Problems
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Preorder Traversal on a Simple Tree
What is the output of the preorder traversal (Root, Left, Right) of the following binary tree?
Tree structure:
1
/ \ 2 3
/
4
Tree structure:
1
/ \ 2 3
/
4
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class Main { public static void preorder(Node root) { if (root == null) return; System.out.print(root.val + " "); preorder(root.left); preorder(root.right); } public static void main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); preorder(root); } }
Attempts:
2 left
💡 Hint
Remember preorder means visit root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits nodes in the order: root, left child, right child. Starting at 1, then left subtree 2 and its left child 4, then right subtree 3.
❓ Predict Output
intermediatePreorder Traversal Output with Null Children
What will be printed by the preorder traversal of this tree?
Tree structure:
5
\ 6
/
7
Tree structure:
5
\ 6
/
7
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class Main { public static void preorder(Node root) { if (root == null) return; System.out.print(root.val + " "); preorder(root.left); preorder(root.right); } public static void main(String[] args) { Node root = new Node(5); root.right = new Node(6); root.right.left = new Node(7); preorder(root); } }
Attempts:
2 left
💡 Hint
Preorder visits root first, then left, then right.
✗ Incorrect
Start at 5, then left child is null so skip, then right child 6, then 6's left child 7.
🔧 Debug
advancedIdentify the Error in Preorder Traversal Code
What error will this code produce when running preorder traversal on a non-empty tree?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class Main { public static void preorder(Node root) { if (root == null) return; preorder(root.left); System.out.print(root.val + " "); preorder(root.right); } public static void main(String[] args) { Node root = new Node(10); root.left = new Node(20); root.right = new Node(30); preorder(root); } }
Attempts:
2 left
💡 Hint
Check the order of printing and recursive calls.
✗ Incorrect
The code prints after visiting left subtree, so it is an inorder traversal, not preorder. Output is left, root, right: 20 10 30.
❓ Predict Output
advancedPreorder Traversal Output of a Larger Tree
What is the preorder traversal output of this tree?
Tree structure:
8
/ \ 3 10
/ \ \ 1 6 14
/ \ / 4 7 13
Tree structure:
8
/ \ 3 10
/ \ \ 1 6 14
/ \ / 4 7 13
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class Main { public static void preorder(Node root) { if (root == null) return; System.out.print(root.val + " "); preorder(root.left); preorder(root.right); } public static void main(String[] args) { Node root = new Node(8); root.left = new Node(3); root.right = new Node(10); root.left.left = new Node(1); root.left.right = new Node(6); root.left.right.left = new Node(4); root.left.right.right = new Node(7); root.right.right = new Node(14); root.right.right.left = new Node(13); preorder(root); } }
Attempts:
2 left
💡 Hint
Remember preorder visits root first, then left subtree, then right subtree.
✗ Incorrect
Start at 8, then left subtree 3, then 3's left 1, then 3's right 6 and its children 4 and 7, then right subtree 10, then 10's right 14 and its left 13.
🧠 Conceptual
expertNumber of Nodes Visited in Preorder Traversal
Given a binary tree with 15 nodes, how many nodes will be visited during a complete preorder traversal?
Attempts:
2 left
💡 Hint
Preorder traversal visits every node exactly once.
✗ Incorrect
Preorder traversal visits every node in the tree exactly once, so all 15 nodes are visited.
