/* * 145. Binary Tree Postorder Traversal * 2015.11.21 by Mingyang * 左右中,反过来就是中左右,所以就相当于inorder反过来,稍稍改进一下就好 */ public List<Integer> postorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>(); if (root == null) return list; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode p = root; while (!stack.isEmpty() || p != null) { if (p != null) { stack.push(p); list.add(p.val); p = p.right; } else { TreeNode t = stack.pop(); p = t.left; } } Collections.reverse(list); return list; } public List<Integer> postorderTraversal1(TreeNode root) { List<Integer> res=new ArrayList<Integer>(); dfs(root,res); return res; } public void dfs1(TreeNode root,List<Integer> res){ if(root==null) return; dfs1(root.left,res); dfs1(root.right,res); res.add(root.val); }