Challenge - 5 Problems
Postorder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of the postorder traversal?
Given the binary tree below, what is the output of the postorder traversal (Left, Right, Root)?
Tree structure:
1
/ \
2 3
/ \
4 5
Tree structure:
1
/ \
2 3
/ \
4 5
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function postorder(root) { if (!root) return []; return [...postorder(root.left), ...postorder(root.right), root.value]; } const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); console.log(postorder(root));
Attempts:
2 left
💡 Hint
Remember postorder means visit left subtree, then right subtree, then the root node.
✗ Incorrect
In postorder traversal, we visit left child, then right child, then the node itself. So for the tree, the order is 4 (left of 2), 5 (right of 2), 2 (root of left subtree), 3 (right child of root), then 1 (root).
❓ Predict Output
intermediate2:00remaining
What is the postorder traversal output for this tree?
Consider this binary tree:
10
/ \
20 30
/ \
40 50
What is the postorder traversal output (Left, Right, Root)?
10
/ \
20 30
/ \
40 50
What is the postorder traversal output (Left, Right, Root)?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function postorder(root) { if (!root) return []; return [...postorder(root.left), ...postorder(root.right), root.value]; } const root = new Node(10); root.left = new Node(20); root.right = new Node(30); root.right.left = new Node(40); root.right.right = new Node(50); console.log(postorder(root));
Attempts:
2 left
💡 Hint
Postorder visits left subtree first, then right subtree, then root.
✗ Incorrect
The traversal visits 20 (left child), then 40 and 50 (left and right of 30), then 30, and finally 10 (root).
🧠 Conceptual
advanced1:30remaining
Which statement about postorder traversal is true?
Choose the correct statement about postorder traversal of a binary tree.
Attempts:
2 left
💡 Hint
Think about when the root node is visited in postorder.
✗ Incorrect
Postorder traversal always visits the left subtree first, then the right subtree, and finally the root node.
🔧 Debug
advanced2:00remaining
Find the error in this postorder traversal code
The following code is intended to perform a postorder traversal of a binary tree and return an array of node values. What is the error?
DSA Javascript
function postorder(root) {
if (!root) return [];
return [root.value, ...postorder(root.left), ...postorder(root.right)];
}Attempts:
2 left
💡 Hint
Check the order in which root and children are visited.
✗ Incorrect
The code visits the root first, then left and right children, which is preorder traversal, not postorder.
🚀 Application
expert1:30remaining
How many nodes are visited before the root in postorder traversal?
In a postorder traversal of a binary tree with 7 nodes, how many nodes are visited before the root node is visited?
Attempts:
2 left
💡 Hint
Postorder visits all children before the root.
✗ Incorrect
Postorder traversal visits all nodes in the left and right subtrees before visiting the root node, so all other nodes (6) are visited first.