@@ -0,0 +1,1534 @@
+/***%0A * Given a binary tree, return the postorder traversal of its nodes' values.%0A *%0A * For example:%0A * Given binary tree %7B1,#,2,3%7D,%0A *    1%0A *     %5C%0A *      2%0A *     /%0A *    3%0A * return %5B3,2,1%5D.%0A */%0A/**%0A * Definition for binary tree%0A * public class TreeNode %7B%0A *     int val;%0A *     TreeNode left;%0A *     TreeNode right;%0A *     TreeNode(int x) %7B val = x; %7D%0A * %7D%0A */%0Apublic class Solution %7B%0A    // Solution 1: Recursive O(n)%0A    public List%3CInteger%3E postorderTraversal(TreeNode root) %7B%0A        List%3CInteger%3E ret = new ArrayList%3CInteger%3E();%0A        dfs(ret, root);%0A        return ret;%0A    %7D%0A%0A    private void dfs(List%3CInteger%3E ret, TreeNode root) %7B%0A        if (root == null)%0A            return;%0A        dfs(ret, root.left);%0A        dfs(ret, root.right);%0A        ret.add(root.val);%0A    %7D%0A%0A    // Solution 2: Iterative O(n)%0A    public List%3CInteger%3E postorderTraversal(TreeNode root) %7B%0A        List%3CInteger%3E ret = new ArrayList%3CInteger%3E();%0A        TreeNode n = root, p;%0A        Stack%3CTreeNode%3E stk = new Stack%3CTreeNode%3E();%0A%0A        do%7B%0A            while (n != null) %7B%0A                stk.push(n);%0A                n = n.left;%0A            %7D%0A            p = null;%0A            while (!stk.isEmpty()) %7B%0A                n = stk.pop();%0A                if (n.right == p) %7B%0A                    ret.add(n.val);%0A                    p = n;%0A                %7D else %7B%0A                    stk.push(n);%0A                    n = n.right;%0A                    break;%0A                %7D%0A            %7D%0A        %7Dwhile(!stk.isEmpty());%0A        return ret;%0A    %7D%0A%7D%0A

add BinaryTreePostorderTraversal.java