Construct Binary Tree from Inorder and Postorder Traversal - Complete Solution Guide
Construct Binary Tree from Inorder and Postorder Traversal is LeetCode problem 106, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree . Example 1: Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] Output: [3,9,20,null,null,15,7] Example 2: Input: inorder = [-1], postorder = [-1] Output: [-1] Constraints: 1 <= inorder.length <= 3000 postorder.length == inorder.length -3000 <= inorder[i], postorder[i] <= 3000 inorder and post
Detailed Explanation
The problem asks us to reconstruct a binary tree given its inorder and postorder traversals. The inorder traversal lists nodes in the order: left subtree, root, right subtree. The postorder traversal lists nodes in the order: left subtree, right subtree, root. We need to return the root of the constructed tree. The input consists of two integer arrays, `inorder` and `postorder`, representing these traversals. The problem guarantees that the values are unique and that both traversals belong to the same tree.
Solution Approach
The solution uses a recursive approach. We start with the root node, which is the last element in the `postorder` array. We find the index of this root node in the `inorder` array. This index divides the `inorder` array into left and right subtrees. We then recursively build the left and right subtrees using the corresponding portions of the `inorder` and `postorder` arrays. A hash map (or array in C) is used to efficiently find the index of the root in the `inorder` array, making the lookup O(1).
Step-by-Step Algorithm
- Step 1: If either `inorder` or `postorder` is empty, return `None` (or `NULL`).
- Step 2: Create a hash map (or array in C) to store the index of each value in the `inorder` array. This allows for O(1) lookup of the root's position in `inorder`.
- Step 3: Initialize a `post_idx` variable to the last index of the `postorder` array. This variable keeps track of the current root node being processed.
- Step 4: Define a recursive helper function that takes the left and right indices of the `inorder` array as input. Base case: if the left index is greater than the right index, it indicates that we have reached an empty subtree and should return `None` (or `NULL`).
- Step 5: In the helper function, create a new `TreeNode` with the value from `postorder[post_idx]`. Decrement `post_idx`.
- Step 6: Find the index of the root value in the `inorder` array using the hash map.
- Step 7: Recursively call the helper function to build the right subtree, passing the indices from `inorder_idx + 1` to `in_right`. Assign the result to the right child of the current node. Crucially, the right subtree is built *before* the left subtree, because `post_idx` is decrementing from the *end* of the `postorder` array.
- Step 8: Recursively call the helper function to build the left subtree, passing the indices from `in_left` to `inorder_idx - 1`. Assign the result to the left child of the current node.
- Step 9: Return the current node.
Key Insights
- The last element in the `postorder` array is always the root of the (sub)tree.
- The `inorder` array allows us to determine the structure of the left and right subtrees. Elements to the left of the root in `inorder` belong to the left subtree, and elements to the right belong to the right subtree.
- Recursion is a natural approach to solve this problem because we can recursively build the left and right subtrees.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Divide and Conquer, Tree, Binary Tree.
Companies
Asked at: Adobe.