Challenge - 5 Problems
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this preorder traversal?
Given the binary tree below, what is the preorder traversal output?
Tree structure:
1
/ \
2 3
/ / \
4 5 6
Tree structure:
1
/ \
2 3
/ / \
4 5 6
DSA C#
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } void Preorder(Node root) { if (root == null) return; Console.Write(root.val + " "); Preorder(root.left); Preorder(root.right); } // Tree creation Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.right.left = new Node(5); root.right.right = new Node(6); Preorder(root);
Attempts:
2 left
💡 Hint
Remember preorder visits root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits nodes in Root-Left-Right order. Starting at 1, then left subtree (2, then 4), then right subtree (3, then 5 and 6).
🧠 Conceptual
intermediateWhich node is visited second in preorder traversal?
In preorder traversal (Root, Left, Right), which node is visited second in the tree below?
Tree:
10
/ \
20 30
/
40
Tree:
10
/ \
20 30
/
40
Attempts:
2 left
💡 Hint
Preorder visits root first, then left child.
✗ Incorrect
Preorder visits root (10) first, then left child (20) second.
🔧 Debug
advancedWhy does this preorder traversal code cause a stack overflow?
Consider this preorder traversal code snippet. Why does it cause a stack overflow when run on a large tree?
Code:
void Preorder(Node root) { if (root == null) return; Console.Write(root.val + " "); Preorder(root.left); Preorder(root.right); }
Code:
void Preorder(Node root) { if (root == null) return; Console.Write(root.val + " "); Preorder(root.left); Preorder(root.right); }
Attempts:
2 left
💡 Hint
Think about how recursion uses the call stack.
✗ Incorrect
The recursive calls add to the call stack for each node. For very deep trees, this can exceed the stack size and cause overflow.
❓ Predict Output
advancedWhat is the preorder traversal output of this unbalanced tree?
Given the tree below, what is the preorder traversal output?
Tree structure:
7
\
8
\
9
/
10
Tree structure:
7
\
8
\
9
/
10
DSA C#
class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } void Preorder(Node root) { if (root == null) return; Console.Write(root.val + " "); Preorder(root.left); Preorder(root.right); } Node root = new Node(7); root.right = new Node(8); root.right.right = new Node(9); root.right.right.left = new Node(10); Preorder(root);
Attempts:
2 left
💡 Hint
Preorder visits root, then left subtree, then right subtree.
✗ Incorrect
Start at 7, no left child, go right to 8, then right to 9, then left to 10.
🚀 Application
expertHow many nodes are visited in preorder traversal of this tree?
Given the tree below, how many nodes will be visited during preorder traversal?
Tree structure:
15
/ \
10 20
/ / \
8 17 25
\
19
Tree structure:
15
/ \
10 20
/ / \
8 17 25
\
19
Attempts:
2 left
💡 Hint
Count all nodes in the tree.
✗ Incorrect
Nodes are 15,10,8,20,17,19,25 totaling 7 nodes.
