145. Binary Tree Postorder Traversal

Binary Tree Postorder Traversal (LeetCode 145) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Postorder Traversal subpattern.

How to solve Binary Tree Postorder Traversal

Binary Tree Postorder Traversal is a Postorder Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master postorder traversal for tree height, diameter, and path sum problems. Process children before root.

Work towards the solution in these steps:

  1. Hint 1: Recall that postorder traversal visits left subtree, then right subtree, then the root node.
  2. Hint 2: Recursive solutions are straightforward. Can you simulate the call stack with an explicit stack for an iterative approach?
  3. Hint 3: For iterative postorder, consider using two stacks or modifying preorder traversal (root-right-left) and reversing the result.

The reference solution runs in O(n) time and O(1) space.

The Binary Tree Postorder Traversal solution and editorial walks through the full approach with reference code.

Problem statement

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?

Topics and companies

Topics: Stack, Tree, Depth-First Search, Binary Tree.

Reported in interviews at Amazon.

How to practise Binary Tree Postorder Traversal on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Binary Tree Postorder Traversal in the code editor, or read the Binary Tree Postorder Traversal editorial for a worked solution with its approach and complexity analysis.

Where Binary Tree Postorder Traversal sits in the DSA pattern sheet

Problems related to Binary Tree Postorder Traversal

Other problems that use the same Recursive Postorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:

Binary Tree Postorder Traversal FAQ

What is the LeetCode number of Binary Tree Postorder Traversal?

Binary Tree Postorder Traversal is LeetCode problem 145, rated Easy.

What pattern is used to solve Binary Tree Postorder Traversal?

Binary Tree Postorder Traversal is a Postorder Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Postorder Traversal subpattern.

What is the time complexity of Binary Tree Postorder Traversal?

The reference solution runs in O(n) time and O(1) space.

Which companies ask Binary Tree Postorder Traversal?

Binary Tree Postorder Traversal has been reported in interviews at Amazon.

Preparing this workspace.