Tree Traversal Postorder Left Right Root in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to visit all nodes in a tree grows as the tree gets bigger.
Specifically, we ask: How long does postorder traversal take as the number of nodes increases?
Analyze the time complexity of the following code snippet.
function postorderTraversal(node: TreeNode | null): void {
if (node === null) return;
postorderTraversal(node.left);
postorderTraversal(node.right);
console.log(node.value);
}
This code visits every node in the tree by first visiting the left child, then the right child, and finally the node itself.
- Primary operation: Recursive calls visiting each node once.
- How many times: Exactly once per node in the tree.
Each node is visited once, so the total work grows 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 complete the traversal grows directly in proportion to 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 exactly once, even though the order is left, right, then root. The recursion does not repeat nodes.
Understanding this traversal's time complexity helps you explain how tree algorithms scale, a key skill in many coding challenges.
"What if we changed the traversal to visit nodes in preorder (root, left, right)? How would the time complexity change?"
Practice
Solution
Step 1: Understand postorder traversal definition
Postorder traversal visits the left subtree first, then the right subtree, and finally the root node.Step 2: Match the traversal order to options
Left subtree, Right subtree, Root node correctly lists Left subtree, Right subtree, Root node order.Final Answer:
Left subtree, Right subtree, Root node -> Option AQuick Check:
Postorder = Left, Right, Root [OK]
- Confusing postorder with preorder or inorder
- Visiting root before children
- Swapping left and right subtree order
Solution
Step 1: Identify correct parameter and return type for traversal
Traversal functions usually take a node or null and return void if printing or processing inline.Step 2: Check options for correct signature
function postorder(node: TreeNode | null): void takes a node or null and returns void, suitable for traversal printing or processing.Final Answer:
function postorder(node: TreeNode | null): void -> Option DQuick Check:
Traversal function signature = function postorder(node: TreeNode | null): void [OK]
- Omitting null in parameter type
- Returning incorrect types like TreeNode or number
- Missing parameter in function signature
1 / \ 2 3 / \ 4 5
What is the output of a postorder traversal?
Solution
Step 1: Traverse left subtree of root (1)
Left subtree rooted at 2: postorder visits 4, then 5, then 2.Step 2: Traverse right subtree and root
Right subtree is 3, visited next. Finally, root 1 is visited.Final Answer:
[4, 5, 2, 3, 1] -> Option AQuick Check:
Postorder output = [4, 5, 2, 3, 1] [OK]
- Visiting root before children
- Mixing left and right subtree order
- Confusing with preorder or inorder output
function postorder(node: TreeNode | null): void {
if (!node) return;
postorder(node.right);
postorder(node.left);
console.log(node.val);
}What is the error in this code?
Solution
Step 1: Analyze traversal order in code
The function calls postorder on right child first, then left child, then prints root.Step 2: Compare with correct postorder order
Correct postorder is left, right, root. This code reverses left and right order.Final Answer:
It visits right subtree before left, reversing postorder -> Option CQuick Check:
Left before right in postorder [OK]
- Swapping left and right subtree calls
- Printing before visiting children
- Ignoring null checks
Solution
Step 1: Understand postorder traversal collection
Postorder visits left, then right, then root. Values should be pushed after children.Step 2: Check each option for correct order and pushing
function postorder(node: TreeNode | null, result: number[]): void { if (!node) return; postorder(node.left, result); postorder(node.right, result); result.push(node.val); } visits left, right, then pushes node value, correctly collecting postorder.Final Answer:
function postorder(node: TreeNode | null, result: number[]): void { if (!node) return; postorder(node.left, result); postorder(node.right, result); result.push(node.val); } -> Option BQuick Check:
Push after children = postorder [OK]
- Pushing node value before children
- Returning new arrays instead of using passed array
- Swapping left and right subtree calls
Ready to prove it?
Pass the test and this pattern moves unknown
