Challenge - 5 Problems
Postorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Postorder Traversal on a Simple Binary Tree
What is the output of the postorder traversal (Left, Right, Root) of the following binary tree?
Tree structure:
1
/ \
2 3
/ \
4 5
Tree structure:
1
/ \
2 3
/ \
4 5
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def postorder(root): if root is None: return [] return postorder(root.left) + postorder(root.right) + [root.val] root = Node(1) root.left = Node(2) root.right = Node(3) root.right.left = Node(4) root.right.right = Node(5) result = postorder(root) print(result)
Attempts:
2 left
💡 Hint
Remember postorder means visit left subtree, then right subtree, then the root node.
✗ Incorrect
Postorder traversal visits nodes in the order: left child, right child, then the node itself. For the given tree, left subtree of root (1) is node 2, which has no children, so output 2 first. Then right subtree of root is node 3, whose left child is 4 and right child is 5, so visit 4, then 5, then 3. Finally visit root 1. So the output is [2, 4, 5, 3, 1].
🧠 Conceptual
intermediateNumber of Nodes 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, regardless of the tree shape. So for 7 nodes, it visits all 7 nodes.
❓ Predict Output
advancedPostorder Traversal Output for Unbalanced Tree
What is the output of the postorder traversal for this unbalanced binary tree?
Tree structure:
10
/
5
\
7
\
9
Tree structure:
10
/
5
\
7
\
9
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def postorder(root): if root is None: return [] return postorder(root.left) + postorder(root.right) + [root.val] root = Node(10) root.left = Node(5) root.left.right = Node(7) root.left.right.right = Node(9) result = postorder(root) print(result)
Attempts:
2 left
💡 Hint
Follow left subtree first, then right subtree, then root.
✗ Incorrect
The left subtree of 10 is 5, which has no left child but has right child 7, which itself has right child 9. Postorder visits left (none), then right subtree recursively, then node. So order is 9, 7, 5, then root 10.
🔧 Debug
advancedIdentify the Error in Postorder Traversal Code
What error will this code produce when performing postorder traversal?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def postorder(root): if root is None: return [] return postorder(root.right) + postorder(root.left) + [root.val] root = Node(1) root.left = Node(2) root.right = Node(3) print(postorder(root))
Attempts:
2 left
💡 Hint
Check the order of recursive calls in the return statement.
✗ Incorrect
The code visits right subtree first, then left subtree, then root. This reverses the left and right order, so output is [3, 2, 1] instead of correct postorder [2, 3, 1]. No syntax or runtime error occurs.
🚀 Application
expertPostorder Traversal Output After Node Removal
Given the binary tree below, if the node with value 3 and its subtree are removed, what is the postorder traversal output?
Tree structure:
1
/ \
2 3
/ \
4 5
Tree structure:
1
/ \
2 3
/ \
4 5
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def postorder(root): if root is None: return [] return postorder(root.left) + postorder(root.right) + [root.val] root = Node(1) root.left = Node(2) root.right = Node(3) root.right.left = Node(4) root.right.right = Node(5) # Remove node 3 and its subtree root.right = None result = postorder(root) print(result)
Attempts:
2 left
💡 Hint
Removing node 3 removes its entire subtree. Only nodes 1 and 2 remain.
✗ Incorrect
After removing node 3 and its children, the tree has root 1 with left child 2 only. Postorder visits left subtree (2), then right subtree (none), then root (1). So output is [2, 1].
