Problem 144
Binary Tree Postorder Traversal
Easy · 10 pts · Tree, Stack, Binary Tree
Problem
You are given a binary tree. Print its postorder traversal: for every node, first visit its whole left subtree, then its whole right subtree, and only then the node itself. The root is always visited last. A recursive function does this directly. Iteratively, you can do a modified preorder (node, right, left) with a stack and then reverse the result.
Input
First line: integer N, the number of tokens. Second line: N tokens describing the tree in level order, where 'null' marks a missing child (the same format LeetCode uses).
Output
Print the values in postorder, space-separated.
Constraints
1 <= number of nodes <= 10000; -100 <= value <= 100
Example input
4 1 null 2 3
Example output
3 2 1
Explanation
The root 1 has a right child 2, and 2 has a left child 3. Postorder finishes the subtree of 2 first, visiting 3 and then 2, and visits the root 1 last. The order is 3 2 1.