package leetcode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;

/**
 * https://leetcode.com/problems/binary-tree-postorder-traversal/
 */
public class Problem145 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Set<TreeNode> set = new HashSet<>();
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        boolean right = false;
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            while (node.left == null) {
                TreeNode n = node.left;
                stack.add(n);
            }
            
            System.out.println("popping: " + node.val);
            while ((node.left == null || set.contains(node.left))
                && (node.right == null || set.contains(node.right))) {
                System.out.println(node.val);
                result.add(node.val);
                set.add(node);
            }
            if (node.left != null) {
                System.out.println("adding: " + node.left.val);
                stack.add(node.left);
                right = false;
            } else {
                right = true;
                if (right) {
                    if (node.right != null) {
                        System.out.println("adding: " + node.right.val);
                        stack.add(node.right);
                    }
                }
            }
        }
        return result;
    }

    private List<Integer> recursivePostOrderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        recursivePostOrderTraversal(root, result);
        return result;
    }

    private void recursivePostOrderTraversal(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        recursivePostOrderTraversal(root.left, result);
        recursivePostOrderTraversal(root.right, result);
        result.add(root.val);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.left.right.right = new TreeNode(6);

        Problem145 prob = new Problem145();
//        System.out.println(prob.recursivePostOrderTraversal(root));
        System.out.println(prob.postorderTraversal(root));
    }
}

@@ -503,32 +503,97 @@
  ArrayList%3C%3E();%0A
+        if (root == null) %7B%0A            return result;%0A        %7D%0A
         Set%3CTree
@@ -697,39 +697,8 @@
 t);%0A
-        boolean right = false;%0A
     
@@ -785,206 +785,10 @@
     
-while (node.left == null) %7B%0A                TreeNode n = node.left;%0A                stack.add(n);%0A            %7D%0A            %0A            System.out.println(%22popping: %22 + node.val);%0A            while
+if
  ((n
@@ -923,35 +923,20 @@
     
-System.out.println
+set.add
 (node
-.val
 );%0A 
@@ -984,37 +984,32 @@
             
-set.add(node)
+continue
 ;%0A          
@@ -1028,416 +1028,204 @@
     
-if (node.left != null) %7B%0A                System.out.println(%22adding: %22 + node.left.val);%0A                stack.add(node.left);%0A                right = false;%0A            %7D else %7B%0A                right = true;%0A                if (right) %7B%0A                    if (node.right != null) %7B%0A                        System.out.println(%22adding: %22 + node.right.val);%0A                        stack.add(node.right);%0A    
+stack.add(node);%0A            if (node.left == null %7C%7C set.contains(node.left)) %7B%0A                if (node.right != null) %7B%0A                    stack.add(node.right);%0A                    continue;%0A
     
@@ -1230,36 +1230,32 @@
               %7D%0A
-    
             %7D%0A  
@@ -1268,1011 +1268,167 @@
     
-%7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    private List%3CInteger%3E recursivePostOrderTraversal(TreeNode root) %7B%0A        List%3CInteger%3E result = new ArrayList%3C%3E();%0A        recursivePostOrderTraversal(root, result);%0A        return result;%0A    %7D%0A%0A    private void recursivePostOrderTraversal(TreeNode root, List%3CInteger%3E result) %7B%0A        if (root == null) %7B%0A            return;%0A        %7D%0A        recursivePostOrderTraversal(root.left, result);%0A        recursivePostOrderTraversal(root.right, result);%0A        result.add(root.val
+TreeNode n = node.left;%0A            while (n != null) %7B%0A                stack.add(n
 );%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        TreeNode root = new TreeNode(1);%0A        root.left = new TreeNode(2);%0A        root.right = new TreeNode(3);%0A        root.left.left = new TreeNode(4);%0A        root.left.right = new TreeNode(5);%0A        root.left.right.right = new TreeNode(6);%0A%0A        Problem145 prob = new Problem145();%0A//        System.out.println(prob.recursivePostOrderTraversal(root));%0A        System.out.println(prob.postorderTraversal(root))
+        n = n.left;%0A            %7D%0A        %7D%0A        return result
 ;%0A  

Solve problem 145