public ArrayList<Integer> postorderTraversal(TreeNode root) {
		ArrayList<Integer> res = new ArrayList<Integer>();
	    if(root==null)
	        return res;
	    LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
	    stack.addFirst(root);
	    TreeNode pre = null;
	    while(!stack.isEmpty()){
	        TreeNode cur = stack.peekFirst();
	        // from top down
	        if(pre == null || cur == pre.left || cur == pre.right){
	            //only add one children, and left go first
	            if(cur.left!=null){
	                stack.addFirst(cur.left);
	            }
	            else if(cur.right!=null){
	                stack.addFirst(cur.right);
	            }
	        }
	        // from bottom up
	        else if(pre == cur.left){
	            if(cur.right!=null)
	                stack.addFirst(cur.right);
	        }
	        else{
	            res.add(cur.val);
	            stack.pollFirst();
	        }
	        pre = cur;
	    }
	    return res;
	}