@@ -0,0 +1,3315 @@
+public class Solution %7B%0A    public static  ArrayList%3CInteger%3E inorderTraversal(TreeNode root) %7B%0A        // IMPORTANT: Please reset any member data you declared, as%0A        // the same Solution instance will be reused for each test case.%0A         ArrayList%3CInteger%3E lst = new ArrayList%3CInteger%3E();%0A %0A        if(root == null)%0A            return lst; %0A %0A        Stack%3CTreeNode%3E stack = new Stack%3CTreeNode%3E();%0A        //define a pointer to track nodes%0A        TreeNode p = root;%0A %0A        while(!stack.empty() %7C%7C p != null)%7B%0A %0A            // if it is not null, push to stack%0A            //and go down the tree to left%0A            if(p != null)%7B%0A                stack.push(p);%0A                p = p.left;%0A %0A            // if no left child%0A            // pop stack, process the node%0A            // then let p point to the right%0A            %7Delse%7B%0A                TreeNode t = stack.pop();%0A                lst.add(t.val);%0A                p = t.right;%0A            %7D%0A        %7D%0A %0A        return lst;%0A    %7D%0A    %0A    public static ArrayList%3CInteger%3E postorderTraversal(TreeNode root) %7B%0A    %09 %0A        ArrayList%3CInteger%3E lst = new ArrayList%3CInteger%3E();%0A %0A        if(root == null)%0A            return lst; %0A %0A        Stack%3CTreeNode%3E stack = new Stack%3CTreeNode%3E();%0A        stack.push(root);%0A %0A        TreeNode prev = null;%0A        while(!stack.empty())%7B%0A            TreeNode curr = stack.peek();%0A %0A            // go down the tree.%0A            //check if current node is leaf, if so, process it and pop stack,%0A            //otherwise, keep going down%0A            if(prev == null %7C%7C prev.left == curr %7C%7C prev.right == curr)%7B%0A                //prev == null is the situation for the root node%0A                if(curr.left != null)%7B%0A                    stack.push(curr.left);%0A                %7Delse if(curr.right != null)%7B%0A                    stack.push(curr.right);%0A                %7Delse%7B%0A                    stack.pop();%0A                    lst.add(curr.val);%0A                %7D%0A %0A            //go up the tree from left node    %0A            //need to check if there is a right child%0A            //if yes, push it to stack%0A            //otherwise, process parent and pop stack%0A            %7Delse if(curr.left == prev)%7B%0A                if(curr.right != null)%7B%0A                    stack.push(curr.right);%0A                %7Delse%7B%0A                    stack.pop();%0A                    lst.add(curr.val);%0A                %7D%0A %0A            //go up the tree from right node %0A            //after coming back from right node, process parent node and pop stack. %0A            %7Delse if(curr.right == prev)%7B%0A                stack.pop();%0A                lst.add(curr.val);%0A            %7D%0A %0A            prev = curr;%0A        %7D%0A %0A        return lst;%0A    %7D%0A%0A    %0A    public static void main(String%5B%5D args)%7B%0A    %09TreeNode node1 = new TreeNode(1);%0A    %09TreeNode node2 = new TreeNode(2);%0A    %09TreeNode node3 = new TreeNode(3);%0A    %09TreeNode node4 = new TreeNode(4);%0A    %09TreeNode node5 = new TreeNode(5);%0A    %09%0A    %09node2.setLeft(node4);%0A    %09node2.setRight(node5);%0A    %09%0A    %09node1.setLeft(node2);%0A    %09node1.setRight(node3);%0A    %09%0A    %09List%3CInteger%3E result = Solution.inorderTraversal(node1);%0A    %09%0A    %09System.out.println(result);%0A    %09%0A    %09 result = Solution.postorderTraversal(node1);%0A     %09%0A     %09System.out.println(result);%0A    %09%0A    %09%0A    %09%0A    %09%0A    %7D%0A%7D%0A

Create solution.java