Binary Tree Postorder Traversal (Recursive)

Easy Trees & BST Recursion Original on LeetCode

Binary Tree Postorder Traversal (Recursive) is a easy trees & bst problem solved with the recursion pattern. The best approach, iterative (reverse of root-right-left), runs in O(n) time and O(n) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from recursive dfs up.

Problem

Return the postorder traversal (left, right, node) of a binary tree.

Examples

Example 1

Input
root = [4, 2, 6, 1, 3]
Output
[1, 3, 2, 6, 4]

Example 2

Input
root = [1]
Output
[1]

Constraints

  • The tree has 0 to 100 nodes.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 12 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Recursive DFSO(n)O(h)
Iterative (reverse of root-right-left)O(n)O(n)

1Recursive DFS

TimeO(n)
SpaceO(h)

Visit both children, then record the node.

  1. if node == null return.
  2. postorder(left); postorder(right); add(node.val).
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> out = new ArrayList<>();
        postorder(root, out);
        return out;
    }

    private void postorder(TreeNode node, List<Integer> out) {
        if (node == null) return;
        postorder(node.left, out);
        postorder(node.right, out);
        out.add(node.val);
    }
}

2Iterative (reverse of root-right-left)

TimeO(n)
SpaceO(n)

A root, right, left preorder is exactly postorder reversed. Run that with a stack and add each value to the front of the list.

  1. Push root; pop, addFirst(val), push left, push right.
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        LinkedList<Integer> out = new LinkedList<>();
        if (root == null) return out;
        Deque<TreeNode> st = new ArrayDeque<>();
        st.push(root);
        while (!st.isEmpty()) {
            TreeNode n = st.pop();
            out.addFirst(n.val);
            if (n.left != null) st.push(n.left);
            if (n.right != null) st.push(n.right);
        }
        return out;
    }
}

Edge cases to test

  • Empty tree

Hints

Hint 1

Postorder = left, right, then node. A node is handled only after both its subtrees, which is why height, diameter and balance checks use it.

FAQ

What is the best time complexity for Binary Tree Postorder Traversal (Recursive)?

Iterative (reverse of root-right-left) runs in O(n) time and O(n) extra space.

Which pattern does Binary Tree Postorder Traversal (Recursive) use?

It is a trees & bst problem that uses the recursion pattern. Other problems with the same pattern: Binary Tree Inorder Traversal (Recursive), Binary Tree Preorder Traversal (Recursive), Binary Tree Inorder Traversal (Iterative).

Is there a brute force solution for Binary Tree Postorder Traversal (Recursive)?

Yes. Recursive DFS takes O(n) time and O(h) space. Visit both children, then record the node.

Which edge cases should I test for Binary Tree Postorder Traversal (Recursive)?

Empty tree.