Lesson guide
What this Python exercise practices
Implement postorder traversal is a beginner practice lesson that focuses on dsa, problem patterns, edge cases. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python functions
- Loops
- Lists
- Basic edge cases
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Related public exercises
Summary
Implement postorder (left-right-root) traversal for a binary tree, returning node values in a list.
Problem statement
Given the root of a binary tree, implement a function postorder(root) that returns a list of node values visited in postorder (left, right, root). Use the provided Node class. Return an empty list for an empty tree.
Task
Write postorder(root) to traverse left subtree, then right subtree, then visit root, returning a list of the values.
Examples
Three-node tree
Input
postorder(Node(1, Node(2), Node(3)))
Output
[2, 3, 1]
Explanation
Visit left 2, right 3, then root 1.
Input format
A single argument: root (Node or None).
Output format
A list of integers representing node values in postorder order.
Constraints
Nodes up to 10^4. Node values are integers. Use recursion or iteration. Return list, do not print.
Samples
Sample input 0
postorder(None)
Sample output 0
[]
Explanation 0
Empty tree returns empty list.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.