Challenge - 5 Problems
Postorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of 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
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn postorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if let Some(node) = root {
postorder(&node.left, result);
postorder(&node.right, result);
result.push(node.val);
}
}
fn main() {
let root = Some(Box::new(Node {
val: 1,
left: Some(Box::new(Node {
val: 2,
left: Some(Box::new(Node { val: 4, left: None, right: None })),
right: Some(Box::new(Node { val: 5, left: None, right: None })),
})),
right: Some(Box::new(Node { val: 3, left: None, right: None })),
}));
let mut result = Vec::new();
postorder(&root, &mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember postorder means visit left subtree, then right subtree, then the root node.
✗ Incorrect
Postorder traversal visits nodes in Left, Right, Root order. For the given tree, left subtree of root 1 is subtree rooted at 2, which itself has left 4 and right 5. So traversal order is 4, 5, 2, then right subtree 3, then root 1.
🧠 Conceptual
intermediateWhich traversal order matches postorder?
Select the correct order of visiting nodes in postorder traversal of a binary tree.
Attempts:
2 left
💡 Hint
Postorder traversal visits children before the parent.
✗ Incorrect
Postorder traversal visits the left subtree first, then the right subtree, and finally the root node.
🔧 Debug
advancedIdentify the error in this postorder traversal code
What error will this Rust code produce when compiling?
Code snippet:
fn postorder(root: &Option>, result: &mut Vec) {
if root.is_some() {
postorder(&root.as_ref().unwrap().left, result);
postorder(&root.as_ref().unwrap().right, result);
result.push(root.unwrap().val);
}
}
DSA Rust
fn postorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if root.is_some() {
postorder(&root.as_ref().unwrap().left, result);
postorder(&root.as_ref().unwrap().right, result);
result.push(root.unwrap().val);
}
}Attempts:
2 left
💡 Hint
Check how unwrap() is used on a borrowed reference.
✗ Incorrect
Calling unwrap() on &Option> tries to move the value out of a borrowed reference, which Rust forbids. This causes a compile error.
🚀 Application
advancedHow many nodes are visited in postorder traversal?
Given a binary tree with 7 nodes, how many nodes will be visited during a postorder traversal?
Attempts:
2 left
💡 Hint
Traversal visits every node exactly once.
✗ Incorrect
Postorder traversal visits every node in the tree exactly once, so the count equals the total nodes.
❓ Predict Output
expertWhat is the output of this postorder traversal with missing children?
Consider the binary tree below with some missing children:
10
/
5
\
7
What is the output of the postorder traversal (Left, Right, Root)?
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn postorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if let Some(node) = root {
postorder(&node.left, result);
postorder(&node.right, result);
result.push(node.val);
}
}
fn main() {
let root = Some(Box::new(Node {
val: 10,
left: Some(Box::new(Node {
val: 5,
left: None,
right: Some(Box::new(Node { val: 7, left: None, right: None })),
})),
right: None,
}));
let mut result = Vec::new();
postorder(&root, &mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember to visit left subtree, then right subtree, then root.
✗ Incorrect
Node 5 has no left child but has right child 7. Postorder visits left (none), then right (7), then node 5, then root 10.
