<p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>Input:</strong> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= preorder.length &lt;= 3000</code></li>
	<li><code>inorder.length == preorder.length</code></li>
	<li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 3000</code></li>
	<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
	<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
	<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
	<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
        def _buildTree(pre_start, pre_end, in_start, in_end):
            if pre_start == pre_end:
                return TreeNode(preorder[pre_start])

            m = preorder[pre_start]
            m_inorder_idx = vals_idx[m]
            left_size = m_inorder_idx - in_start
            right_size = in_end - m_inorder_idx

            # for i in range(in_start, in_end + 1):
            #     if inorder[i] == m:
            #         m_inorder_idx = i
            #         left_size = i - in_start
            #         right_size = in_end - i
            #         break
            
            m_node = TreeNode(m)

            pre_left_start = pre_start + 1
            pre_left_end = pre_left_start + left_size - 1
            pre_right_start = pre_left_end + 1
            pre_right_end = pre_end

            in_left_start = in_start
            in_left_end = m_inorder_idx - 1
            in_right_start = m_inorder_idx + 1
            in_right_end = in_end

            if left_size > 0:
                m_node.left = _buildTree(pre_left_start, pre_left_end, in_left_start, in_left_end)
            
            if right_size > 0:
                m_node.right = _buildTree(pre_right_start, pre_right_end, in_right_start, in_right_end)
            return m_node
    
        vals_idx = {inorder[i]: i for i in range(len(inorder))}
        return _buildTree(0, len(preorder) - 1, 0, len(inorder) - 1)
            
        




























    def buildTree2(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:    
        def _buildTree(pstart, pend, istart, iend):
            if iend < istart:
                return None
            
            root_val = preorder[pstart]

            i = inorder_index_map[root_val]
            
            ileft_start = istart
            ileft_end = i - 1
            iright_start = i + 1
            iright_end = iend
            
            left_size = ileft_end - istart + 1
            right_size = iend - iright_start + 1

            pleft_start = pstart + 1
            pleft_end = pleft_start + left_size - 1
            pright_start = pleft_end + 1
            pright_end = pend
            
            left = _buildTree(pleft_start, pleft_end, ileft_start, ileft_end) if left_size else None
            right = _buildTree(pright_start, pright_end, iright_start, iright_end) if right_size else None
            return TreeNode(root_val, left, right)
        
        inorder_index_map = {letter: i for i, letter in enumerate(inorder)}
        return _buildTree(0, len(preorder) - 1, 0, len(inorder) - 1)

19334