Class Solution
Medium
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
Example 1:

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Example 2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
Constraints:
1 <= preorder.length <= 3000inorder.length == preorder.length-3000 <= preorder[i], inorder[i] <= 3000preorderandinorderconsist of unique values.- Each value of
inorderalso appears inpreorder. preorderis guaranteed to be the preorder traversal of the tree.inorderis guaranteed to be the inorder traversal of the tree.
To solve the “Construct Binary Tree from Preorder and Inorder Traversal” problem in Java with a Solution class, we’ll use a recursive approach. Below are the steps:
-
Create a
Solutionclass: Define a class namedSolutionto encapsulate our solution methods. -
Create a
buildTreemethod: This method takes two integer arrays,preorderandinorder, as input and returns the constructed binary tree. -
Check for empty arrays: Check if either of the arrays
preorderorinorderis empty. If so, return null, as there’s no tree to construct. -
Define a helper method: Define a recursive helper method
buildto construct the binary tree.- The method should take the indices representing the current subtree in both
preorderandinorder. - The start and end indices in
preorderrepresent the current subtree’s preorder traversal. - The start and end indices in
inorderrepresent the current subtree’s inorder traversal.
- The method should take the indices representing the current subtree in both
-
Base case: If the start index of
preorderis greater than the end index or if the start index ofinorderis greater than the end index, return null. -
Find the root node: The root node is the first element in the
preorderarray. -
**Find the root’s position in
inorder**: Iterate through theinorderarray to find the root’s position. -
Recursively build left and right subtrees:
- Recursively call the
buildmethod for the left subtree with updated indices. - Recursively call the
buildmethod for the right subtree with updated indices.
- Recursively call the
-
Return the root node: After constructing the left and right subtrees, return the root node.
Here’s the Java implementation:
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length == 0 || inorder.length == 0) return null; // Check for empty arrays
return build(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1); // Construct binary tree
}
// Recursive helper method to construct binary tree
private TreeNode build(int[] preorder, int[] inorder, int preStart, preEnd, int inStart, int inEnd) {
if (preStart > preEnd || inStart > inEnd) return null; // Base case
int rootValue = preorder[preStart]; // Root node value
TreeNode root = new TreeNode(rootValue); // Create root node
// Find root node's position in inorder array
int rootIndex = 0;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == rootValue) {
rootIndex = i;
break;
}
}
// Recursively build left and right subtrees
root.left = build(preorder, inorder, preStart + 1, preStart + rootIndex - inStart, inStart, rootIndex - 1);
root.right = build(preorder, inorder, preStart + rootIndex - inStart + 1, preEnd, rootIndex + 1, inEnd);
return root; // Return root node
}
// TreeNode definition
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
This implementation follows the steps outlined above and efficiently constructs the binary tree from preorder and inorder traversals in Java.
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
get
public int get(int key) -
buildTree
-