<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:
    TreeNode* solve(vector<int>& preorder, int preStart, int preEnd, vector<int>& postorder, int postStart, int postEnd, unordered_map<int,int>& mp1, unordered_map<int,int>& mp2){
        // base case
        if(preStart > preEnd || postStart > postEnd) return NULL;
        if(preStart == preEnd){
            int root_val = preorder[preStart];
            TreeNode* root = new TreeNode(root_val);
            root->left = NULL;
            root->right = NULL;
            return root;
        }
        if(postStart == postEnd){
            int root_val = preorder[postStart];
            TreeNode* root = new TreeNode(root_val);
            root->left = NULL;
            root->right = NULL;
            return root;
        }

        int root_val = preorder[preStart];
        TreeNode* root = new TreeNode(root_val);
        int leftNodes = mp2[preorder[preStart+1]] - postStart + 1;
        int rightNodes = (postorder[postEnd-1]!=preorder[preStart+1]) ? preEnd - mp1[postorder[postEnd-1]] + 1 : 0;

        root->left = solve(preorder, preStart+1, preStart+leftNodes, postorder, postStart, postStart+leftNodes-1, mp1, mp2);
        root->right = solve(preorder, preEnd-rightNodes+1, preEnd, postorder, postEnd-rightNodes, postEnd-1, mp1, mp2);

        return root;
    }
public:
    TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
        int n1 = preorder.size();
        int n2 = postorder.size();
        unordered_map<int,int> mp1, mp2;
        for(int i=0; i<n1; i++) mp1[preorder[i]] = i;
        for(int i=0; i<n2; i++) mp2[postorder[i]] = i;
        return solve(preorder,0,n1-1,postorder,0,n2-1,mp1,mp2);
    }
};
29600