描述
Given a binary tree, return the postorder traversal of its nodes’ values.
Example:
1 2 3 4 5 6 7 8
| Input: [1,null,2,3] 1 \ 2 / 3
Output: [3,2,1]
|
Follow up: Recursive solution is trivial, could you do it iteratively?
分析
后序遍历二叉树。
中序遍历二叉树
前序遍历二叉树
解决方案1(Java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
class Solution { public List<Integer> postorderTraversal(TreeNode root) { ArrayList<Integer> result = new ArrayList<Integer>(); postOrder(root, result); return result; } private void postOrder(TreeNode root, ArrayList<Integer> result) { if (root == null) { return; } postOrder(root.left, result); postOrder(root.right, result); result.add(root.val); } }
|
相关问题