Java Real Postorder Solution
We use a prev pointer to record the last visited node. \nIf prev is the last children of the stack\'s top node, or stack\'s top node has no children, then we ca
We use a `prev` pointer to record the last visited node. \nIf `prev` is the last children of the stack\'s top node, or stack\'s top node has no children, then we can safely visit stack\'s top node.\nOtherwise, we need to push all children of the stack\'s top node, and visit the original stack\'s top later, until all children are visited.\n\n> Why so many people think the reverse preorder solution is a good one? It\'s just a trick, not postorder!\n\n```java\nclass Solution {\n    public List<Integer> postorder(Node root) {\n        List<Integer> result = new ArrayList<>();\n        Deque<Node> stack = new ArrayDeque<>();\n        \n        if(root == null){\n            return result;\n        }\n        \n        stack.offerFirst(root);\n        Node prev = null;\n        while(!stack.isEmpty()){\n            Node cur = stack.peekFirst();\n\t\t\t// check if it\'s time to visit the stack\'s top\n\t\t\t// only when it\'s children are all visited, or it has no children\n            if(cur.children.size() == 0 || prev == cur.children.get(cur.children.size() - 1)){\n\t\t\t\t//visit the stack.s top element and update prev pointer\n                cur = stack.pollFirst();\n\t\t\t\tresult.add(cur.val);      \n                prev = cur;                 \n            } else {\n                for(int i = cur.children.size() - 1; i >=0; i--){\n                    stack.offerFirst(cur.children.get(i));\n                }\n            }\n        }\n        return result;\n    }\n\n}\n```
[]