<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder 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> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>

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

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

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

<ul>
	<li><code>1 &lt;= inorder.length &lt;= 3000</code></li>
	<li><code>postorder.length == inorder.length</code></li>
	<li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li>
	<li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li>
	<li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li>
	<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
	<li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder 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, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:

        if len(inorder) == 0:
            return None
        if len(inorder) == 1:
            return TreeNode(inorder[0])
        
        inorder_nodes = {
            val: dict(node=TreeNode(val), index=i) for i, val in enumerate(inorder)
        }
        postorder_nodes = {
            val: dict(node=inorder_nodes[val]["node"], index=i)
            for i, val in enumerate(postorder)
        }

        def is_parent(current_val: int, comparison_val: int) -> bool:
            return postorder_nodes[current_val]["index"] < postorder_nodes[comparison_val]["index"]

        def get_left_child(poi: int) -> TreeNode:
            val = postorder[poi]
            ini = inorder_nodes[val]["index"]
            if ini == 0 or poi == 0:
                return None
            ini -= 1
            left_val = inorder[ini]
            if is_parent(val, left_val):
                return None

            right_val = val
            ini = inorder_nodes[val]["index"]
            if ini < len(inorder) - 1 and not is_parent(val, inorder[ini + 1]):
                right_val = inorder[ini + 1]

            child_poi = postorder_nodes[right_val]["index"] - 1
            child_val = postorder[child_poi]
            while inorder_nodes[val]["index"] < inorder_nodes[child_val]["index"]:
                if child_poi > 0:
                    child_poi -= 1
                    child_val = postorder[child_poi]
                else:
                    child_val = inorder[inorder_nodes[child_val]["index"] - 1]

            return inorder_nodes[child_val]["node"]

        def get_right_child(poi: int) -> TreeNode:
            val = postorder[poi]
            ini = inorder_nodes[val]["index"]
            if inorder_nodes[val]["index"] == len(inorder) - 1 or poi == 0:
                return None
            ini += 1
            right_val = inorder[ini]
            if is_parent(val, right_val):
                return None
            child_val = postorder[poi-1]
            if inorder_nodes[child_val]["index"] < inorder_nodes[val]["index"]:
                return None
            return inorder_nodes[child_val]["node"]

        for i in range(len(postorder) - 1, -1, -1):
            val = postorder[i]
            node = postorder_nodes[val]["node"]
            node.left = get_left_child(i)
            node.right = get_right_child(i)

        return postorder_nodes[postorder[-1]]["node"]


"""

            3

    9               20


4       5       15      7
                    
    13
                11            6


inordrer = [4, 13 9, 5, 3, 11, 15, 20, 7, 6]
postorder = [13, 4, 5, 9, 11, 15, 6, 7, 20, 3]



i_count, p_count = 0, 0

node = TreeNode(postorder[p_count])

if postorder[p_count] != inorder[i_count]:
    


if postorder[0] == inorder[0]:
    i_count += 1
    p_count += 1
    new_node = TreeNode(inorder[i_count], left=node)
    node = new_node





"""
19300