非递归 public class Solution { public List<Integer> postorderTraversal(TreeNode root) { if (null == root) { return new ArrayList<>(); } ArrayList<Integer> res = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); stack.add(root); while (!stack.isEmpty()) { TreeNode node = stack.pop(); res.add(0, node.val); if (node.left != null) { stack.push(node.left); } if (node.right != null) { stack.push(node.right); } } return res; } } 递归 public class Solution { ArrayList<Integer> res = new ArrayList<>(); public List<Integer> postorderTraversal(TreeNode root) { postOrder(root); return res; } public void postOrder(TreeNode root) { if (null == root) { return; } // 前序 postOrder(root.left); // 中序 postOrder(root.right); // 后序 res.add(root.val); } }