145_Binary Tree Postorder Traversal
145. Binary Tree Postorder Traversal
Level: hard
Tag: tree, stack
Question
Given a binary tree, return the postorder traversal of its nodes' values.Example 1
Given binary tree [1,null,2,3],
1
\
2
/
3
return [3,2,1]Solution 1: recursive
Solution 2: iteratively (my solution, easy to understand)
use stack to store node value, right child, and left child repeatedly. When node value is popped out, add to result, otherwise repeat.
Another version, popular online
return node value -> right child -> left child repeatedly, and then reverse the result.
Another version, hard to understand
Last updated