public ArrayList<Integer> postorderTraversal(TreeNode root) {
		// Start typing your Java solution below
		// DO NOT write main() function
		ArrayList<Integer> res = new ArrayList<Integer>();
		if (root == null)
			return res;

		Stack<TreeNode> path = new Stack<TreeNode>();
		TreeNode prev = null, cur;
		path.push(root);
		while (!path.isEmpty()) {
			cur = path.peek();
			if (prev == null || prev.left == cur || prev.right == cur) {
				if (cur.left != null)
					path.push(cur.left);
				else if (cur.right != null)
					path.push(cur.right);
			} else if (cur.left == prev) {
				if (cur.right != null)
					path.push(cur.right);
			} else {
				res.add(cur.val);
				path.pop();
			}
			prev = cur;
		}

		return res;
	}