Challenge - 5 Problems
Postorder Mastery
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 following Java code performing postorder traversal (Left, Right, Root) on the given binary tree?
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class Main { static void postorder(Node root) { if (root == null) return; postorder(root.left); postorder(root.right); System.out.print(root.data + " "); } public static void main(String[] args) { Node 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); postorder(root); } }
Attempts:
2 left
💡 Hint
Remember postorder traversal visits left subtree, then right subtree, then the root node.
✗ Incorrect
Postorder traversal visits nodes in Left, Right, Root order. For the tree:
1
/ \
2 3
/ \
4 5
The traversal order is 4 (left of 2), 5 (right of 2), 2 (root of left subtree), 3 (right subtree), 1 (root).
🧠 Conceptual
intermediateNumber of Nodes Visited Before Root in Postorder Traversal
In postorder traversal (Left, Right, Root) of a binary tree with 7 nodes, how many nodes are visited before the root node is processed?
Attempts:
2 left
💡 Hint
Postorder visits all children before the root.
✗ Incorrect
Postorder traversal visits all nodes in left and right subtrees before visiting the root. So for 7 nodes, the root is visited last after the other 6 nodes.
🔧 Debug
advancedIdentify the Error in Postorder Traversal Code
What error will the following Java code produce when performing postorder traversal?
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class Main { static void postorder(Node root) { if (root == null) return; postorder(root.right); postorder(root.left); System.out.print(root.data + " "); } public static void main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); postorder(root); } }
Attempts:
2 left
💡 Hint
Check the order of recursive calls for left and right subtrees.
✗ Incorrect
The code visits right subtree first, then left subtree, then root. This is a reversed postorder traversal (Right, Left, Root). It runs without error and outputs 3 2 1.
❓ Predict Output
advancedOutput of Postorder Traversal on Unbalanced Tree
What is the output of the postorder traversal on this unbalanced binary tree?
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class Main { static void postorder(Node root) { if (root == null) return; postorder(root.left); postorder(root.right); System.out.print(root.data + " "); } public static void main(String[] args) { Node root = new Node(10); root.right = new Node(20); root.right.right = new Node(30); postorder(root); } }
Attempts:
2 left
💡 Hint
Postorder visits left, then right, then root. Left subtree is empty here.
✗ Incorrect
The tree structure is:
10
\
20
\
30
Postorder visits left (none), right subtree (30), then root (20), then root (10). So output is 30 20 10.
🚀 Application
expertNumber of Recursive Calls in Postorder Traversal
Given a full binary tree with 15 nodes, how many total recursive calls to the postorder function are made during a complete postorder traversal?
Attempts:
2 left
💡 Hint
Each node causes calls for itself and its null children.
✗ Incorrect
Each node calls postorder on left and right child plus itself. For a full binary tree with 15 nodes, there are 16 null children (leaves have null children). Total calls = nodes + null calls = 15 + 16 = 31.
