Iterative Post-Order Traversal
Hard
Implement an iterative, post-order traversal of a given binary tree, return the list of keys of each node in the tree as it is post-order traversed.
Examples
5
/ \
3 8
/ \ \
1 4 11
Post-order traversal is [1, 4, 3, 11, 8, 5]
Corner Cases
What if the given binary tree is null? Return an empty list in this case.
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
Solution: self, left, right check, then flip
since the goal is to obtain nodes in order of
left child
right child
self
We use a stack to perform
poll self
offer left child
offer right child
Since stacks are LIFO, this will result in
add self
add right
add left
Finally reverse the results
TC: O(N)
SC: O(N)
Last updated
Was this helpful?