DSA Tracker

Problem

Easy

Binary Tree Postorder Traversal

An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Binary Trees
Sheets
3
Core for
4 roles

The problem

Collect the values of a binary tree in postorder: for every node, record its left subtree, then its right subtree, and the node itself last. Return them as a list.

Example 1

Input
[5,3,7,1,4,null,8]
Output
[1,4,3,8,7,5]
Why
Postorder: left subtree 1,4,3 then right 8,7 then root 5.

Example 2

Input
[]
Output
[]
Why
An empty tree produces an empty traversal.

Example 3

Input
[7,4,9]
Output
[4,9,7]
Why
Left 4, then right 9, then the root 7.

Constraints

  • The number of nodes is in the range [0, 100].
  • -100 <= Node.val <= 100

How to think about it

Updated 2026-09-29

A node cannot be harvested until both of its children are completely finished and done. Postorder is bottom-up aggregation: leaves report first, and the root is the very last element processed. Symmetrically, reading root-right-left and reversing the final sequence yields exact postorder.

Approaches, worst first

  1. Standard recursion

    time O(n) · space O(n)

    Recurse into left child, recurse into right child, then push node value into result. Straightforward but tied to system call stack depth.

  2. Two stacks or reverse modified preorder

    time O(n) · space O(n)

    Perform a preorder variant visiting root-right-left and push into an intermediate collector. Reversing the output flips the collection into left-right-root at the cost of allocating two full buffers.

  3. Single stack with last visited trackingWrite this one

    time O(n) · space O(n)

    Traverse leftwards onto a stack. Peek the top node: if its right subtree exists and has not just been processed, pivot right. Otherwise, pop and emit the node, recording it as last-visited to avoid re-entering its right child.

Where people lose marks · 3
  • Popping a parent node before inspecting its right child causes the right subtree to be skipped or visited out of order.
  • Without tracking `lastVisited`, re-peeking a parent node after returning from its right child re-enters the right subtree indefinitely.
  • Not returning an empty list on null root input.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

More Binary Trees problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .