Challenge - 5 Problems
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this preorder traversal?
Given the binary tree below, what is the output of the preorder traversal (Root, Left, Right)?
DSA Javascript
class Node { constructor(value, left = null, right = 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, result = []) { if (!node) return result; result.push(node.value); preorder(node.left, result); preorder(node.right, result); return result; } console.log(preorder(root).join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Remember preorder visits root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits nodes in the order: root, left child, then right child.
Starting at 10 (root), then left subtree 5, then 3 and 7, then right subtree 15 and 18.
❓ Predict Output
intermediate1:00remaining
Output of preorder traversal on a single-node tree
What is the output of the preorder traversal on this tree with only one node?
DSA Javascript
class Node { constructor(value, left = null, right = null) { this.value = value; this.left = left; this.right = right; } } const root = new Node(42); function preorder(node, result = []) { if (!node) return result; result.push(node.value); preorder(node.left, result); preorder(node.right, result); return result; } console.log(preorder(root).join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Preorder always includes the root node first.
✗ Incorrect
With only one node, preorder traversal visits that node and then ends.
🔧 Debug
advanced1:30remaining
Identify the error in this preorder traversal function
What error will this preorder traversal code produce when run?
DSA Javascript
function preorder(node, result = []) {
if (node == null) return;
result.push(node.value);
preorder(node.left, result);
preorder(node.right, result);
return result;
}
const root = null;
console.log(preorder(root));Attempts:
2 left
💡 Hint
Check what happens when the function returns without a value.
✗ Incorrect
The function returns undefined when node is null because it returns without a value.
This causes the final console.log to print undefined.
❓ Predict Output
advanced2:00remaining
Preorder traversal output with unbalanced tree
What is the output of the preorder traversal for this unbalanced tree?
DSA Javascript
class Node { constructor(value, left = null, right = null) { this.value = value; this.left = left; this.right = right; } } const root = new Node(1, new Node(2, new Node(4), null ), new Node(3, null, new Node(5, null, new Node(6) ) ) ); function preorder(node, result = []) { if (!node) return result; result.push(node.value); preorder(node.left, result); preorder(node.right, result); return result; } console.log(preorder(root).join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Preorder visits root first, then left subtree, then right subtree.
✗ Incorrect
Start at 1, then left subtree 2 and 4, then right subtree 3, 5, and 6.
🧠 Conceptual
expert1:30remaining
How many nodes are visited in preorder traversal of a full binary tree with height 3?
A full binary tree is a tree where every node has 0 or 2 children. If the tree has height 3 (root is height 1), how many nodes will the preorder traversal visit?
Attempts:
2 left
💡 Hint
Number of nodes in a full binary tree of height h is 2^h - 1.
✗ Incorrect
A full binary tree of height 3 has 2^3 - 1 = 7 nodes.
Preorder traversal visits all nodes, so it visits 7 nodes.