Challenge - 5 Problems
Postorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Postorder Traversal on a Simple Binary Tree
What is the output of the postorder traversal (Left, Right, Root) for the given binary tree?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func postorder(root *Node, result *[]int) { if root == nil { return } postorder(root.Left, result) postorder(root.Right, result) *result = append(*result, root.Val) } func main() { root := &Node{Val: 1} root.Left = &Node{Val: 2} root.Right = &Node{Val: 3} root.Left.Left = &Node{Val: 4} root.Left.Right = &Node{Val: 5} var result []int postorder(root, &result) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Remember postorder means visit left subtree, then right subtree, then the root node.
✗ Incorrect
In postorder traversal, we first visit the left child nodes, then the right child nodes, and finally the root node. For this tree, the order is 4 (leftmost leaf), 5 (right child of 2), 2 (left child of root), 3 (right child of root), and finally 1 (root).
🧠 Conceptual
intermediate1:00remaining
Understanding Postorder Traversal Steps
Which of the following best describes the order of visiting nodes in postorder traversal?
Attempts:
2 left
💡 Hint
Postorder traversal always visits the root last.
✗ Incorrect
Postorder traversal visits the left subtree first, then the right subtree, and finally the root node.
🔧 Debug
advanced2:00remaining
Identify the Bug in Postorder Traversal Implementation
What error will this code produce when performing postorder traversal?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func postorder(root *Node, result *[]int) { if root == nil { return } postorder(root.Right, result) postorder(root.Left, result) *result = append(*result, root.Val) } func main() { root := &Node{Val: 1} root.Left = &Node{Val: 2} root.Right = &Node{Val: 3} var result []int postorder(root, &result) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Check the order of recursive calls for left and right children.
✗ Incorrect
The code visits the right subtree first, then the left subtree, then the root. This reverses the left and right order, so the output is not a correct postorder traversal.
🚀 Application
advanced2:00remaining
Postorder Traversal Output for Unbalanced Tree
Given the following unbalanced binary tree, what is the postorder traversal output?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func postorder(root *Node, result *[]int) { if root == nil { return } postorder(root.Left, result) postorder(root.Right, result) *result = append(*result, root.Val) } func main() { root := &Node{Val: 10} root.Right = &Node{Val: 20} root.Right.Left = &Node{Val: 15} root.Right.Right = &Node{Val: 25} var result []int postorder(root, &result) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Remember to visit left subtree, then right subtree, then root.
✗ Incorrect
The left child of 20 is 15, right child is 25. Postorder visits 15, then 25, then 20, then finally 10.
🧠 Conceptual
expert0:30remaining
Number of Nodes Visited in Postorder Traversal
If a binary tree has 7 nodes, how many nodes will be visited during a complete postorder traversal?
Attempts:
1 left
💡 Hint
Postorder traversal visits every node exactly once.
✗ Incorrect
Postorder traversal visits every node in the tree exactly once, so the number of nodes visited equals the total number of nodes.