<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) {}
 * };
 */

void connect(TreeNode* node, int preorder[], int start1, int postorder[], int start2, int len) {
    node->val = preorder[start1];
    // cout << len << endl;
    // cout << start1 << ";" << start2 << endl;
    for (int i = start1; i<start1+len; i++) cout << preorder[i] << " ";
    cout << endl;
    for (int i = start2; i<start2+len; i++) cout << postorder[i] << " ";
    cout << endl;
    if (len == 1)
        return;
    TreeNode* right_node = new TreeNode;
    right_node->val = postorder[start2 + len - 2];
    if (right_node->val == preorder[start1 + 1]) {
        node->right = right_node;
        connect(right_node, preorder, start1 + 1, postorder, start2, len-1);
        return;
    }
    TreeNode* left_node = new TreeNode;
    int right_child_index = 0;
    for (int i = 0; i < len; i++) {
        if (preorder[start1+i] == right_node->val) {
            right_child_index = i;
            break;
        }
    }
    node->right = right_node;
    node->left = left_node;
    // cout << right_child_index << " " << right_node->val << endl;
    // cout << "---" << start1+right_child_index-1 << endl;
    connect(right_node, preorder, start1+right_child_index, postorder, start2+right_child_index-1, len-right_child_index);
    connect(left_node, preorder, start1+1, postorder, start2, right_child_index-1);
}

class Solution {
public:
    TreeNode* constructFromPrePost(vector<int>& preorder,
                                   vector<int>& postorder) {
        TreeNode* root = new TreeNode;
        root->val = preorder[0];
        int len = preorder.size();
        int pre_order[len], post_order[len];
        for(int i=0; i<len; i++) {
            pre_order[i] = preorder[i];
            post_order[i] = postorder[i];
        }
        connect(root, pre_order, 0, post_order, 0, len);
        return root;
    }
};
28100