方法一:递归的方法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> arraylist=new ArrayList<Integer>(); if(root==null){ return arraylist; } List<Integer> left=postorderTraversal(root.left); List<Integer> right=postorderTraversal(root.right); arraylist.addAll(left); arraylist.addAll(right); arraylist.add(root.val); return arraylist; } }
方法二:迭代的方法