LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal Solution

Master LeetCode problem 105 (Construct Binary Tree from Preorder and Inorder Traversal), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

105. Construct Binary Tree from Preorder and Inorder Traversal

Problem Explanation

Explanation

To construct a binary tree from preorder and inorder traversals, we can utilize the following approach:

  1. The first element of the preorder traversal array will always be the root of the binary tree.
  2. Find the index of the root element in the inorder traversal array.
  3. Elements to the left of the root index in the inorder array will form the left subtree, and elements to the right will form the right subtree.
  4. Recursively build the left and right subtrees using the above information.

Time complexity: O(n) where n is the number of nodes in the binary tree. Space complexity: O(n) for the recursive stack.

Solution Code

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int val) {
        this.val = val;
    }
}

class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeHelper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
    }

    private TreeNode buildTreeHelper(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {
        if (preStart > preEnd || inStart > inEnd) {
            return null;
        }

        int rootVal = preorder[preStart];
        TreeNode root = new TreeNode(rootVal);

        int rootIndex = 0;
        for (int i = inStart; i <= inEnd; i++) {
            if (inorder[i] == rootVal) {
                rootIndex = i;
                break;
            }
        }

        int leftSubtreeSize = rootIndex - inStart;

        root.left = buildTreeHelper(preorder, preStart + 1, preStart + leftSubtreeSize, inorder, inStart, rootIndex - 1);
        root.right = buildTreeHelper(preorder, preStart + leftSubtreeSize + 1, preEnd, inorder, rootIndex + 1, inEnd);

        return root;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 105 (Construct Binary Tree from Preorder and Inorder Traversal)?

This page provides optimized solutions for LeetCode problem 105 (Construct Binary Tree from Preorder and Inorder Traversal) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 105 (Construct Binary Tree from Preorder and Inorder Traversal)?

The time complexity for LeetCode 105 (Construct Binary Tree from Preorder and Inorder Traversal) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 105 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 105 in Java, C++, or Python.

Back to LeetCode Solutions