<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) {}
 * };
 */
 #include<unordered_map>
class Solution {
public:
    TreeNode* construct(const vector<int>& preorder, const vector<int>& postorder, 
                        int preStart, int preEnd, int postStart, int postEnd){

         if (preStart > preEnd || postStart > postEnd) {
            return nullptr;
        }
        
        // Create the root node (the first element of preorder)
        TreeNode* root = new TreeNode(preorder[preStart]);
        
        // Base case: single node tree
        if (preStart == preEnd) {
            return root;
        }
        
        // Find the root of the left subtree (preorder[preStart + 1])
        int leftRootVal = preorder[preStart + 1];
        
        // Find this left subtree root in postorder
        int leftRootIdx = postStart;
        while (postorder[leftRootIdx] != leftRootVal) {
            leftRootIdx++;
        }
        
        // Calculate the size of the left subtree
        int leftSubtreeSize = leftRootIdx - postStart + 1;
        
        // Recursively construct the left and right subtrees
        root->left = construct(preorder, postorder, preStart + 1, preStart + leftSubtreeSize, 
                               postStart, leftRootIdx);
        root->right = construct(preorder, postorder, preStart + leftSubtreeSize + 1, preEnd, 
                                leftRootIdx + 1, postEnd - 1);
        
        return root;
        // if(st>en || p_index<0){
        //     return NULL;
        // }
        // TreeNode* root=new TreeNode(postorder[p_index]);
        // p_index--;
        // if(st==en){
        //     return root;
        // }
        // int boundryindex=0;
        // if(p_index>=0)
        //     boundryindex=mp[postorder[p_index]];
        // else{
        //     root->left=NULL;
        //     root->right=NULL;
        //     return root;
        // }  
        // root->right=construct(boundryindex,en,p_index,mp,postorder);
        // root->left=construct(st,boundryindex-1,p_index,mp,postorder);
        // return root;
    }
    TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
        unordered_map<int,int> mp;
        for(int i=0;i<preorder.size()-1;i++){
            mp[postorder[i]]=i;
        }
        int post=preorder.size()-1;
        return construct(preorder,postorder,0,post,0,post);
    }
};
29100