106. Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution

In this guide, you will get 106. Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution with the best time and space complexity. The solution to Construct Binary Tree from Inorder and Postorder Traversal problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Construct Binary Tree from Inorder and Postorder Traversal solution in C++
  4. Construct Binary Tree from Inorder and Postorder Traversal solution in Java
  5. Construct Binary Tree from Inorder and Postorder Traversal solution in Python
  6. Additional Resources
106. Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution image

Problem Statement of Construct Binary Tree from Inorder and Postorder Traversal

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 postorder consist of unique values.
Each value of postorder also appears in inorder.
inorder is guaranteed to be the inorder traversal of the tree.
postorder is guaranteed to be the postorder traversal of the tree.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

106. Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution in C++

class Solution {
 public:
  TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
    unordered_map<int, int> inToIndex;

    for (int i = 0; i < inorder.size(); ++i)
      inToIndex[inorder[i]] = i;

    return build(inorder, 0, inorder.size() - 1, postorder, 0,
                 postorder.size() - 1, inToIndex);
  }

 private:
  TreeNode* build(const vector<int>& inorder, int inStart, int inEnd,
                  const vector<int>& postorder, int postStart, int postEnd,
                  const unordered_map<int, int>& inToIndex) {
    if (inStart > inEnd)
      return nullptr;

    const int rootVal = postorder[postEnd];
    const int rootInIndex = inToIndex.at(rootVal);
    const int leftSize = rootInIndex - inStart;

    TreeNode* root = new TreeNode(rootVal);
    root->left = build(inorder, inStart, rootInIndex - 1, postorder, postStart,
                       postStart + leftSize - 1, inToIndex);
    root->right = build(inorder, rootInIndex + 1, inEnd, postorder,
                        postStart + leftSize, postEnd - 1, inToIndex);
    return root;
  }
};
/* code provided by PROGIEZ */

106. Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution in Java

class Solution {
  public TreeNode buildTree(int[] inorder, int[] postorder) {
    Map<Integer, Integer> inToIndex = new HashMap<>();

    for (int i = 0; i < inorder.length; ++i)
      inToIndex.put(inorder[i], i);

    return build(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1, inToIndex);
  }

  TreeNode build(int[] inorder, int inStart, int inEnd, int[] postorder, int postStart, int postEnd,
                 Map<Integer, Integer> inToIndex) {
    if (inStart > inEnd)
      return null;

    final int rootVal = postorder[postEnd];
    final int rootInIndex = inToIndex.get(rootVal);
    final int leftSize = rootInIndex - inStart;

    TreeNode root = new TreeNode(rootVal);
    root.left = build(inorder, inStart, rootInIndex - 1, postorder, postStart,
                      postStart + leftSize - 1, inToIndex);
    root.right = build(inorder, rootInIndex + 1, inEnd, postorder, postStart + leftSize,
                       postEnd - 1, inToIndex);
    return root;
  }
}
// code provided by PROGIEZ

106. Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution in Python

class Solution:
  def buildTree(
      self,
      inorder: list[int],
      postorder: list[int],
  ) -> TreeNode | None:
    inToIndex = {num: i for i, num in enumerate(inorder)}

    def build(
        inStart: int,
        inEnd: int,
        postStart: int,
        postEnd: int,
    ) -> TreeNode | None:
      if inStart > inEnd:
        return None

      rootVal = postorder[postEnd]
      rootInIndex = inToIndex[rootVal]
      leftSize = rootInIndex - inStart

      root = TreeNode(rootVal)
      root.left = build(inStart, rootInIndex - 1,  postStart,
                        postStart + leftSize - 1)
      root.right = build(rootInIndex + 1, inEnd,  postStart + leftSize,
                         postEnd - 1)
      return root

    return build(0, len(inorder) - 1, 0, len(postorder) - 1)
# code by PROGIEZ

Additional Resources

See also  301. Remove Invalid Parentheses LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.