Java solution, no need to reverse the list.

  • 4
    public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> R = new LinkedList<Integer>();
        if (root == null) return R;
        Stack<TreeNode> S = new Stack();
        while (!S.empty()) {
            TreeNode node = S.pop();
            R.add(0, node.val);
            if (node.left != null) S.push(node.left);
            if (node.right != null) S.push(node.right);
        return R;


  • 0

    This method with stack could also be used for Preorder and Inorder without reverse !?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.