<p>Given two integer arrays, <code>preorder</code> and <code>postorder</code> where <code>preorder</code> is the preorder traversal of a binary tree of <strong>distinct</strong> values and <code>postorder</code> is the postorder traversal of the same tree, reconstruct and return <em>the binary tree</em>.</p>

<p>If there exist multiple answers, you can <strong>return any</strong> of them.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/24/lc-prepost.jpg" style="width: 304px; height: 265px;" />
<pre>
<strong>Input:</strong> preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1]
<strong>Output:</strong> [1,2,3,4,5,6,7]
</pre>

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

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

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

<ul>
	<li><code>1 &lt;= preorder.length &lt;= 30</code></li>
	<li><code>1 &lt;= preorder[i] &lt;= preorder.length</code></li>
	<li>All the values of <code>preorder</code> are <strong>unique</strong>.</li>
	<li><code>postorder.length == preorder.length</code></li>
	<li><code>1 &lt;= postorder[i] &lt;= postorder.length</code></li>
	<li>All the values of <code>postorder</code> are <strong>unique</strong>.</li>
	<li>It is guaranteed that <code>preorder</code> and <code>postorder</code> are the preorder traversal and postorder traversal of the same binary tree.</li>
</ul>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    unordered_map<int,int> preOrderMap;
    unordered_map<int,int> postOrderMap;

    void fillPrePostMaps(vector<int>& preorder, vector<int>& postorder)
    {
        for(int i = 0 ; i < preorder.size(); i++)
        {
            preOrderMap[preorder[i]] = i;
        }

        for(int i = 0; i < postorder.size(); i++)
        {
            postOrderMap[postorder[i]] = i;
        }
    }

   TreeNode* BuildTreeCore(vector<int>& preorder, vector<int>& postorder, 
                           int preorderStart, int preorderEnd, 
                           int postorderStart, int postorderEnd)
   {

        if(preorderStart > preorderEnd || postorderStart > postorderEnd)
        {
            return NULL;
        }

        if(preorderStart == preorderEnd || postorderStart == postorderEnd)
        {
            return new TreeNode(preorder[preorderStart], NULL, NULL);
        }

        TreeNode *root = new TreeNode(preorder[preorderStart], NULL, NULL);    
        int lPreorderStart = preorderStart + 1;
        int lPreorderEnd = preOrderMap[postorder[postorderEnd - 1]]-1;
        int lPostorderStart = preorderStart;
        int lPostorderEnd = postOrderMap[preorder[lPreorderStart]];
        
        TreeNode *leftSubTree = BuildTreeCore(preorder, postorder, lPreorderStart, 
                                              lPreorderEnd, postorderStart, lPostorderEnd);

        int rPreorderStart = lPreorderEnd + 1;
        int rPreorderEnd = preorderEnd;
        int rPostorderStart = postOrderMap[preorder[lPreorderStart]] + 1;
        int rPostorderEnd = postorderEnd - 1;

        TreeNode *rightSubTree = BuildTreeCore(preorder, postorder, rPreorderStart, 
                                               preorderEnd, postorderStart, rPostorderEnd);
        root->left = leftSubTree;
        root->right = rightSubTree;
        return  root; 
   }
public:
    TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
        int preorderStart = 0;
        int preorderEnd = preorder.size() -1;
        int postorderStart = 0;
        int postorderEnd = postorder.size() -1; 
        fillPrePostMaps(preorder, postorder);

        return BuildTreeCore(preorder, postorder, preorderStart, preorderEnd, postorderStart, postorderEnd);
    }
};
29700