Start learning free

LESSON 203 OF 321 · BINARY TREES

Post Order Tree Traversal II in Java (Time Complexity: O(n))

Practice O(n) Read the full explanation

You get the root of a binary tree. Return an array of its node values in postorder.

In postorder, each node comes after its two subtrees. Its left subtree comes first. Then comes its right subtree. The node itself comes last.

Trees are written level by level, from top to bottom and left to right. A null marks a missing child.

Example 1:

Input: [5,3,8,1,4,null,9]
     5
    / \
   3   8
  / \   \
 1   4   9

Output: [1,4,3,9,8,5]

Example 2:

Input: [9,6,null,null,2]
     9
    /
   6
    \
     2

Output: [2,6,9]
Explanation: Each node here has only one child.
The child still comes before its parent.

Example 3:

Input: [7]
Output: [7]

Constraints:

  • 1 to 1,000 nodes.
  • Each value is a whole number from 1 to 1,000.
  • No two nodes have the same value.

Follow up: The recursive version is only a few lines. Can you also write it without recursion, with your own stack?

STEP 1 OF 14
    Ctrl + Enter to check

    JAVA · BINARY TREES

    Post Order Tree Traversal in Java, explained

    Understanding the Problem

    The core challenge of this problem is to perform a postorder traversal of a binary tree. In postorder traversal, the nodes are recursively visited in this order: left subtree, right subtree, root node. The significance of this traversal is that it is used in various applications such as expression tree evaluations, and it ensures that child nodes are processed before their respective parent nodes.

    Potential pitfalls include misunderstanding the traversal order and incorrectly handling null nodes.

    Approach

    To solve this problem, we can consider both recursive and iterative approaches:

    Naive Solution: Recursive Approach

    The recursive approach is straightforward and easy to implement. However, it is not optimal in terms of stack space usage, especially for deep trees.

    Optimized Solution: Iterative Approach

    The iterative approach is more complex but avoids the potential stack overflow issues of the recursive approach. We can use a stack to simulate the recursive call stack.

    Iterative Approach Using Two Stacks

    1. Use two stacks: one for processing nodes and another for storing the postorder traversal.

    2. Push the root node to the first stack.

    3. While the first stack is not empty, pop a node, push it to the second stack, and push its left and right children to the first stack.

    4. Finally, pop all nodes from the second stack to get the postorder traversal.

    Algorithm

    Here is a step-by-step breakdown of the iterative algorithm using two stacks:

    1. Initialize two stacks: `stack1` and `stack2`.
    2. Push the root node to `stack1`.
    3. While `stack1` is not empty:
      • Pop a node from `stack1` and push it to `stack2`.
      • Push the left child of the popped node to `stack1` (if it exists).
      • Push the right child of the popped node to `stack1` (if it exists).
    4. Pop all nodes from `stack2` and add their values to the result list.

    Code Implementation

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;
    
    public class PostOrderTraversal {
        // Definition for a binary tree node.
        public static class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
            TreeNode(int x) { val = x; }
        }
    
        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 node = stack1.pop();
                stack2.push(node);
                if (node.left != null) stack1.push(node.left);
                if (node.right != null) stack1.push(node.right);
            }
    
            while (!stack2.isEmpty()) {
                result.add(stack2.pop().val);
            }
    
            return result;
        }
    
        public static void main(String[] args) {
            TreeNode root = new TreeNode(1);
            root.right = new TreeNode(2);
            root.right.left = new TreeNode(3);
    
            PostOrderTraversal solution = new PostOrderTraversal();
            List<Integer> result = solution.postorderTraversal(root);
            System.out.println(result); // Output: [3, 2, 1]
        }
    }
    

    Complexity Analysis

    The time complexity of the iterative approach is O(n), where n is the number of nodes in the tree. This is because each node is processed exactly once. The space complexity is also O(n) due to the use of two stacks.

    Edge Cases

    Potential edge cases include:

    • An empty tree (should return an empty list).
    • A tree with only one node.
    • A tree with only left or right subtrees.

    Each of these cases is handled by the algorithm as it checks for null nodes and processes all nodes in the tree.

    Testing

    To test the solution comprehensively, consider the following test cases:

    • Empty tree: `[]`
    • Single node tree: `[1]`
    • Tree with only left children: `[1, 2, 3]`
    • Tree with only right children: `[1, null, 2, null, 3]`
    • Balanced tree: `[1, 2, 3, 4, 5, 6, 7]`

    Use a testing framework like JUnit to automate these tests.

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Understand the traversal order and its significance.
    • Start with a recursive solution to grasp the basic idea.
    • Think about how to simulate recursion using a stack for iterative solutions.
    • Practice similar problems to strengthen your understanding.

    Conclusion

    Postorder traversal is a fundamental tree traversal technique with various applications. Understanding both recursive and iterative approaches is crucial for solving related problems efficiently. Practice and explore further to master these concepts.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free