Challenge - 5 Problems
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Preorder traversal output of a simple binary tree
What is the output of the preorder traversal (Root, Left, Right) of this binary tree?
DSA Typescript
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number, left: Node | null = null, right: Node | null = null) { this.value = value; this.left = left; this.right = right; } } const root = new Node(1, new Node(2), new Node(3)); function preorder(node: Node | null, result: number[] = []): number[] { if (!node) return result; result.push(node.value); preorder(node.left, result); preorder(node.right, result); return result; } console.log(preorder(root));
Attempts:
2 left
💡 Hint
Remember preorder means visit root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits the root node first, then recursively visits the left subtree, and finally the right subtree. Here, root is 1, left child is 2, right child is 3, so output is [1, 2, 3].
❓ Predict Output
intermediate2:00remaining
Preorder traversal output with deeper tree
What is the output of the preorder traversal (Root, Left, Right) of this binary tree?
DSA Typescript
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number, left: Node | null = null, right: Node | null = null) { this.value = value; this.left = left; this.right = right; } } const root = new Node(10, new Node(5, new Node(3), new Node(7)), new Node(15, null, new Node(18))); function preorder(node: Node | null, result: number[] = []): number[] { if (!node) return result; result.push(node.value); preorder(node.left, result); preorder(node.right, result); return result; } console.log(preorder(root));
Attempts:
2 left
💡 Hint
Start from root 10, then go left subtree fully before right subtree.
✗ Incorrect
Preorder visits root first (10), then left subtree (5, 3, 7), then right subtree (15, 18). So output is [10, 5, 3, 7, 15, 18].
🔧 Debug
advanced2:00remaining
Identify the error in preorder traversal implementation
This preorder traversal function is supposed to return an array of node values in preorder. What error will it cause when run?
DSA Typescript
function preorder(node: Node | null, result: number[] = []): number[] {
if (!node) return result;
preorder(node.left, result);
result.push(node.value);
preorder(node.right, result);
return result;
}Attempts:
2 left
💡 Hint
Check the order in which node values are added to the result array.
✗ Incorrect
The function pushes node values after traversing the left subtree, so it visits left subtree first, then root, then right subtree. This is inorder traversal, not preorder.
🧠 Conceptual
advanced1:00remaining
Number of nodes visited in preorder traversal
Given a binary tree with 7 nodes, how many nodes does the preorder traversal visit?
Attempts:
2 left
💡 Hint
Preorder traversal visits every node exactly once.
✗ Incorrect
Preorder traversal visits every node in the tree exactly once, so for 7 nodes, it visits 7 nodes.
🚀 Application
expert3:00remaining
Preorder traversal output of an unbalanced tree
What is the preorder traversal output of this unbalanced binary tree?
DSA Typescript
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number, left: Node | null = null, right: Node | null = null) { this.value = value; this.left = left; this.right = right; } } const root = new Node(8, new Node(3, new Node(1), null), new Node(10, null, new Node(14, new Node(13), null))); function preorder(node: Node | null, result: number[] = []): number[] { if (!node) return result; result.push(node.value); preorder(node.left, result); preorder(node.right, result); return result; } console.log(preorder(root));
Attempts:
2 left
💡 Hint
Follow root, then left subtree, then right subtree carefully.
✗ Incorrect
Preorder visits root 8, then left subtree nodes 3 and 1, then right subtree nodes 10, 14, and 13 in order.