Leetcode 105: Construct Binary Tree from Preorder and Inorder Traversal

grid47
grid47
Exploring patterns and algorithms
Oct 27, 2024 6 min read

A tree gently forming from two intertwining paths of glowing nodes, one representing preorder, the other inorder.
Solution to LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal Problem

Given two integer arrays, preorder and inorder, representing the preorder and inorder traversals of a binary tree, your task is to reconstruct and return the binary tree. The values in the arrays are unique, and the preorder traversal provides the sequence in which nodes are visited before their children, while the inorder traversal provides the order in which nodes are visited between their children.
Problem
Approach
Steps
Complexity
Input: The input consists of two arrays: `preorder` and `inorder`. `preorder` represents the preorder traversal of a binary tree, and `inorder` represents the inorder traversal of the same tree.
Example: preorder = [4,2,1,3,6,5], inorder = [1,2,3,4,5,6]
Constraints:
• 1 <= preorder.length <= 3000
• inorder.length == preorder.length
• -3000 <= preorder[i], inorder[i] <= 3000
• preorder and inorder consist of unique values.
• Each value of inorder also appears in preorder.
• preorder is guaranteed to be the preorder traversal of the tree.
• inorder is guaranteed to be the inorder traversal of the tree.
Output: You should return the root node of the binary tree constructed from the given preorder and inorder traversal arrays.
Example: Output: [4,2,6,1,3,5]
Constraints:
• The output should be the root node of the reconstructed binary tree.
Goal: The goal is to rebuild the binary tree by leveraging the properties of preorder and inorder traversals. In preorder, the first element is always the root, and the elements before it in inorder represent the left subtree, while the elements after it represent the right subtree.
Steps:
• First, create a mapping of each element's index in the inorder array.
• Then recursively pick elements from the preorder array to form the root, left, and right subtrees using the mapping from inorder.
Goal: You need to ensure the solution works efficiently for arrays with up to 3000 elements, given the constraints on the values of the elements.
Steps:
• The solution should handle up to 3000 nodes efficiently.
Assumptions:
• The input arrays represent valid preorder and inorder traversals of the same binary tree.
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Explanation: The binary tree can be reconstructed as follows: root is 3 (first in preorder), the left subtree is [9], and the right subtree is [20,15,7]. The resulting tree is: 3 / \ 9 20 / \ 15 7 This tree matches the given preorder and inorder traversals.

Input: preorder = [1], inorder = [1]
Explanation: The tree consists of a single node with value 1, forming a trivial tree where the preorder and inorder arrays both contain just the value [1].

Input: preorder = [10,5,1,7,15,12,20], inorder = [1,5,7,10,12,15,20]
Explanation: The tree can be reconstructed by recognizing the root (10) from the preorder array, and the left and right subtrees by dividing the inorder array based on the root value.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus