145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
@tag-tree

Algorithm

Since the order of post-order traversal is left-right-root, and the order of pre-order traversal is root-left-right, the two are actually very similar.

You can make some small changes in the method of pre-order traversal to make it work. The order becomes root-right-left, and then flipped, that is, left-right-root. The flip method is to add the result res in the reverse direction.

Each time, when adding to stack, left and then right, so that when the stack is processed, it is right and then left.