<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.
 * 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 {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        TreeNode* root_node = new TreeNode(postorder[postorder.size()-1]);
        vector<int>::iterator it = inorder.begin();
        for(; it != inorder.end(); it++)
        {
            if((*it) == postorder[postorder.size()-1])
                break;
        }

        vector<int>* inorder_left = initialize_left_inorder(it, &inorder);
        vector<int>* inorder_right = initialize_right_inorder(it, &inorder);
        vector<int>* postorder_right = initialize_right_postorder(inorder_right->size(),  &postorder);
        vector<int>* postorder_left = initialize_left_postorder(inorder_right->size(), inorder_left->size(), &postorder);
        
        build_tree(root_node, inorder_left, inorder_right, postorder_left, postorder_right);

        return root_node;
    }

    void build_tree(TreeNode* root, vector<int>* in_left, vector<int>* in_right, vector<int>* post_left, vector<int>* post_right)
    {
        if((in_left->size() == 0 || post_left->size() == 0) && (in_right->size() == 0 || post_right->size() == 0))
            return;

        if(post_left->size() > 0 && in_left->size() > 0)
        {
            vector<int>::iterator it = in_left->begin();
            for(; it != in_left->end(); it++)
            {
                if((*it) == post_left->at(post_left->size()-1))
                    break;
            }
            vector<int>* inorder_left = initialize_left_inorder(it, in_left);
            vector<int>* inorder_right = initialize_right_inorder(it, in_left);
            vector<int>* postorder_right = initialize_right_postorder(inorder_right->size(), post_left);
            vector<int>* postorder_left = initialize_left_postorder(inorder_right->size(),  inorder_left->size(), post_left);

            TreeNode* node = new TreeNode(post_left->at(post_left->size()-1));
            root->left = node;

            build_tree(node, inorder_left, inorder_right, postorder_left, postorder_right);
        }

        if(post_right->size() > 0 && in_right->size() > 0)
        {
            vector<int>::iterator it = in_right->begin();
            for(; it != in_right->end(); it++)
            {
                if((*it) == post_right->at(post_right->size()-1))
                    break;
            }
            vector<int>* inorder_left = initialize_left_inorder(it, in_right);
            vector<int>* inorder_right = initialize_right_inorder(it, in_right);
            vector<int>* postorder_right = initialize_right_postorder(inorder_right->size(), post_right);
            vector<int>* postorder_left = initialize_left_postorder(inorder_right->size(), inorder_left->size(), post_right);

            TreeNode* node = new TreeNode(post_right->at(post_right->size()-1));
            root->right = node;

            build_tree(node, inorder_left, inorder_right, postorder_left, postorder_right);
        }
    }

    vector<int>* initialize_left_inorder(vector<int>::iterator it, vector<int>* left_vector)
    {
        if(it == left_vector->begin())
        {
            //std::cout << "initialize_left_inorder first" << std::endl;
            return new vector<int>();
        }
        else
        {
            //std::cout << "initialize_left_inorder second" << std::endl;
            vector<int>::iterator itr = left_vector->begin();
            /*for(; itr < it; itr++)
                std::cout << (*itr) << ", ";
            std::cout << std::endl;*/
            return new vector<int>(left_vector->begin(), it);
        }
    }

    vector<int>* initialize_right_inorder(vector<int>::iterator it, vector<int>* right_vector)
    {
        if((it+1) != right_vector->end())
        {
            //std::cout << "initialize_right_inorder first" << std::endl;
            vector<int>::iterator itr = it+1;
            /*for(; itr < right_vector->end(); itr++)
                std::cout << (*itr) << ", ";
            std::cout << std::endl;*/
            return new vector<int>(it+1, right_vector->end());
        }
        else
        {
            //std::cout << "initialize_right_inorder second" << std::endl;
            return new vector<int>();
        }
    }

    vector<int>* initialize_left_postorder(int right_size, int left_size, vector<int>* postorder)
    {
        if(left_size > 0)
        {
            //std::cout << "initialize_left_preorder " << left_size << std::endl;
            vector<int>::iterator it = postorder->begin();
            /*(for(; it < postorder->begin()+left_size+1; it++)
                std::cout << (*it) << ", ";
            std::cout << std::endl;*/
            return new vector<int>(postorder->begin(), postorder->begin()+left_size);
        }
        else
        {
            //std::cout << "initialize_left_preorder " << left_size << std::endl;
            return new vector<int>();
        }
    }

    vector<int>* initialize_right_postorder(int right_size, vector<int>* postorder)
    {
        if(right_size > 0)
        {
            //std::cout << "initialize_right_preorder " << right_size << std::endl;
            vector<int>::iterator it = postorder->end()-right_size-1;
            /*for(; it < postorder->end()-1; it++)
                std::cout << (*it) << ", ";
            std::cout << std::endl;*/
            return new vector<int>(postorder->end()-right_size-1, postorder->end()-1);
        }
        else
        {
            //std::cout << "initialize_right_preorder " << right_size << std::endl;
            return new vector<int>();
        }
    }    
};
48021