Binary Tree Postorder Traversal

 Given the root of a binary tree, return the postorder traversal of its nodes' values.


 


Example 1:


Input: root = [1,null,2,3]


Output: [3,2,1]


Explanation:



Example 2:


Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]


Output: [4,6,7,5,2,9,8,3,1]


Explanation:



Example 3:


Input: root = []


Output: []

Example 4:

Input: root = [1]

Output: [1]

Constraints:


The number of the nodes in the tree is in the range [0, 100].

-100 <= Node.val <= 100

 


Follow up: Recursive solution is trivial, could you do it iteratively?


 Steps (Recursive Approach)

  1. Traverse left subtree

  2. Traverse right subtree

  3. Process current node (root)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        postorder(root, result);
        return result;
    }

    private void postorder(TreeNode node, List<Integer> result) {
        if (node == null) return;

        postorder(node.left, result);   // 1. Go Left
        postorder(node.right, result);  // 2. Go Right
        result.add(node.val);           // 3. Process Root
    }
}



Java Code: Iterative Postorder Using 2 Stacks



Option 1: Using Two Stacks

This is the most intuitive and interview-safe iterative method.


🧠 Approach Summary (Two Stack Trick):

  1. Push root into stack1

  2. Pop from stack1, push into stack2

  3. Push left, then right into stack1

  4. At the end, stack2 will have root-right-left, so reverse = postorder


🔁 Why This Works?

  • We push rootrightleft into stack1

  • And pop into stack2

  • So stack2 = root-right-left → reverse it = left-right-root (postorder)

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) return result;

        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();

        stack1.push(root);

        while (!stack1.isEmpty()) {
            TreeNode curr = stack1.pop();
            stack2.push(curr);

            // First left, then right
            if (curr.left != null) {
                stack1.push(curr.left);
            }
            if (curr.right != null) {
                stack1.push(curr.right);
            }
        }

        // Stack2 now contains nodes in postorder (reversed)
        while (!stack2.isEmpty()) {
            result.add(stack2.pop().val);
        }

        return result;
    }
}


Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence