Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes’ values.
(后序遍历二叉树)
Follow up: Recursive solution is trivial, could you do it iteratively?
Example:
1. 递归
具体实现方法如下:
1 | # Definition for a binary tree node. |
2. 迭代 & 栈
由于先序遍历的顺序是根-左-右,后序遍历的顺序是左-右-根,因此可以修改先序遍历的遍历顺序为根-右-左,然后将最终结果倒序即可。具体实现方法如下:
1 | # Definition for a binary tree node. |