class Solution { public List<Integer> postorderTraversal(TreeNode root) { Stack<TreeNode> stack=new Stack<TreeNode>(); List<Integer> res=new ArrayList<Integer>(); while(root!=null||!stack.isEmpty()) { while(root!=null) { res.add(0, root.val); stack.push(root); root=root.right; } root=stack.pop().left; } return res; } }