Tree Traversal Postorder Left Right Root in DSA Python - Time & Space Complexity
We want to understand how the time needed to visit all nodes in a tree grows as the tree gets bigger.
How does the number of steps change when the tree has more nodes?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def postorder(root):
if root is None:
return
postorder(root.left)
postorder(root.right)
print(root.val)
This code visits every node in a tree in postorder: left child, right child, then the node itself.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
Each node is visited exactly once, so the steps grow directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to finish grows directly with the number of nodes in the tree.
[X] Wrong: "Postorder traversal takes longer because it visits nodes multiple times."
[OK] Correct: Each node is visited only once, just in a different order, so time grows linearly.
Understanding traversal time helps you explain how tree algorithms scale and shows you can analyze recursive code clearly.
"What if we changed the traversal to preorder (root, left, right)? How would the time complexity change?"
Practice
Solution
Step 1: Understand postorder traversal definition
Postorder traversal visits nodes in the order: left subtree first, then right subtree, and finally the root node.Step 2: Match the order with options
Left subtree, Right subtree, Root node matches the order Left, Right, Root exactly.Final Answer:
Left subtree, Right subtree, Root node -> Option DQuick Check:
Postorder = Left, Right, Root [OK]
- Confusing postorder with preorder or inorder
- Visiting root node first instead of last
- Swapping left and right subtree order
root?Solution
Step 1: Identify the function name for postorder traversal
The function name should clearly indicate postorder traversal, sopostorderis correct.Step 2: Check the parameter
The parameterrootis standard for the current node in tree traversal functions.Final Answer:
def postorder(root): -> Option AQuick Check:
Function name matches traversal type [OK]
- Using wrong traversal names like preorder or inorder
- Missing the root parameter
- Using generic names that don't specify traversal
1 / \ 2 3 / \ 4 5
What is the output of the postorder traversal?
Solution
Step 1: Traverse left subtree of root (1)
Left subtree rooted at 2: postorder is left (4), right (5), root (2) -> [4, 5, 2]Step 2: Traverse right subtree of root (1)
Right subtree rooted at 3 has no children, so postorder is [3]Step 3: Combine left, right, and root
Full postorder: left subtree [4, 5, 2], right subtree [3], root [1] -> [4, 5, 2, 3, 1]Final Answer:
[4, 5, 2, 3, 1] -> Option AQuick Check:
Postorder = Left, Right, Root [OK]
- Visiting root before children
- Mixing up left and right subtree order
- Listing nodes in preorder or inorder instead
def postorder(root):
if root:
print(root.data)
postorder(root.left)
postorder(root.right)Solution
Step 1: Analyze the order of print and recursive calls
The code prints root.data first, then traverses left and right subtrees. This is preorder traversal order.Step 2: Identify correct postorder sequence
Postorder requires visiting left subtree, then right subtree, then printing root last.Final Answer:
The root node is printed before traversing children -> Option CQuick Check:
Print after children calls for postorder [OK]
- Printing root before children (preorder mistake)
- Not handling empty nodes properly
- Swapping left and right subtree calls
Solution
Step 1: Check the order of appending nodes
def postorder(root): result = [] if root: result += postorder(root.left) result += postorder(root.right) result.append(root.data) return result appends left subtree results, then right subtree results, then root data last, matching postorder.Step 2: Verify return of combined list
def postorder(root): result = [] if root: result += postorder(root.left) result += postorder(root.right) result.append(root.data) return result returns the combined list correctly after recursive calls.Final Answer:
def postorder(root): result = [] if root: result += postorder(root.left) result += postorder(root.right) result.append(root.data) return result -> Option BQuick Check:
Left + Right + Root order in list [OK]
- Appending root before children (preorder)
- Incorrect list concatenation order
- Not returning the combined list properly
Ready to prove it?
Pass the test and this pattern moves unknown
