Advertisement

Binary Tree Postorder Traversal - LeetCode 145 Solution

Binary Tree Postorder Traversal - Complete Solution Guide

Binary Tree Postorder Traversal is LeetCode problem 145, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 iterati

Detailed Explanation

The problem asks for a postorder traversal of a binary tree. Postorder traversal means visiting the left subtree, then the right subtree, and finally the root node. The input is the root node of a binary tree, and the output is a list containing the values of the nodes in postorder sequence. The tree can be empty or contain any number of nodes within the specified constraints. Constraints limit the number of nodes and the range of values within each node.

Solution Approach

The provided solutions use iterative approaches employing a stack to perform the postorder traversal. The Python solution uses a simple stack, pushing nodes and then reversing the result to obtain the postorder sequence. The Java and C++ solutions utilize a more sophisticated iterative approach that avoids the need for reversing the result. This optimized iterative approach cleverly uses a `lastVisited` variable to track the last visited node, ensuring the correct order.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty list/array `result` to store the postorder traversal sequence and a stack `stack`.
  2. Step 2: Handle the base case: If the root is `null` (empty tree), return the empty `result`.
  3. Step 3: (Python): Push the root node onto the stack. While the stack is not empty, pop a node, append its value to `result`, and push its left and right children (if they exist) onto the stack. Finally, reverse `result`.
  4. Step 4: (Java/C++): Push the root node onto the stack. While the stack is not empty or the current node `curr` is not null: If `curr` is not null, push it onto the stack and move to its left child. Otherwise, examine the top node of the stack: If it has a right child that hasn't been visited, move to that right child. Otherwise, add the top node's value to `result`, and pop it. This mimics the recursive call stack precisely.
  5. Step 5: Return the `result` list/array.

Key Insights

  • Insight 1: Postorder traversal inherently requires visiting nodes in a specific order: left, right, then root. This suggests a recursive approach or an iterative approach using a stack to mimic the recursive call stack.
  • Insight 2: Using a stack is crucial for the iterative approach. The stack keeps track of nodes to visit, ensuring the correct order of traversal.
  • Insight 3: Handling the empty tree case is important. A robust solution should gracefully handle an empty input without causing errors.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Stack, Tree, Depth-First Search, Binary Tree.

Companies

Asked at: Google.