id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nint search(vector<int>&inorder,int start,int end,int key){\n for(int i=start;i<=end;i++){\n if(inorder[i]==key)\n return i;\n }\n return -1;\n}\nTreeNode*helper(vector<int>&preorder,vector<int>inorder,int start,int end ,int &idx){\n if(start>end) return nullptr;\n int val=preorder[idx];\n idx++;\n TreeNode*curr=new TreeNode(val);\n if(start==end) return curr;\n int pos=search(inorder,start,end,val);\n curr->left=helper(preorder,inorder,start,pos-1,idx);\n curr->right=helper(preorder,inorder,pos+1,end,idx);\n return curr;\n}\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int n=preorder.size() ;\n int idx=0;\n TreeNode*root=helper(preorder,inorder,0,n-1,idx);\n return root;\n }\n};",
"memory": "115283"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n TreeNode* solve(int &i,int min,int max,vector<int> &preorder,vector<int> inorder){\n if(i>=preorder.size()) return NULL;\n if(max<min){\n --i;\n return NULL;\n }\n if(max == min) return new TreeNode(inorder[max]);\n TreeNode* node = new TreeNode(preorder[i]);\n auto it = find(inorder.begin(), inorder.end(), node->val);\n int index = distance(inorder.begin(), it);\n node->left = solve(++i,min,index-1,preorder,inorder);\n node->right = solve(++i,index+1,max,preorder,inorder);\n\n return node;\n }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int i = 0;\n return solve(i,0,preorder.size()-1,preorder,inorder); \n }\n};",
"memory": "117594"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n private:\n unordered_map<int,int> order;\npublic:\n\n\n TreeNode* building(vector<int> preorder,int& predex,int start,int end)\n {\n if(start>end ) return NULL;\n TreeNode* node=new TreeNode(preorder[predex++]);\n int index=order[node->val];\n node->left=building(preorder,predex,start,index-1);\n node->right=building(preorder,predex,index+1,end);\n return node;\n }\n TreeNode* buildTree(vector<int> preorder, vector<int> inorder) {\n int predex=0,index=0;\n \n for(int i=0;i<inorder.size();i++)\n {\n order[inorder[i]]=i;\n }\n return building(preorder,predex,index,inorder.size()-1);\n }\n};",
"memory": "126839"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "auto init = []() {cin.tie(0); ios::sync_with_stdio(0); return 1;}();\nclass Solution {\npublic:\n int preorderIndex;\n unordered_map<int, int> map;\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n for(int i = 0; i < inorder.size(); i++) {\n map[inorder[i]] = i;\n }\n preorderIndex = 0;\n return build(preorder, 0, inorder.size() - 1);\n }\n\n TreeNode* build(vector<int> preorder, int s, int e) {\n if(s > e) return NULL;\n\n int rootVal = preorder[preorderIndex++];\n TreeNode* root = new TreeNode(rootVal);\n int mid = map[rootVal];\n\n root->left = build(preorder, s, mid - 1);\n root->right = build(preorder, mid + 1, e);\n\n return root;\n }\n};",
"memory": "126839"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return dfs(preorder, inorder);\n }\n\n TreeNode* dfs(vector<int> preorder, vector<int> inorder) {\n if (preorder.size() == 0 || inorder.size() == 0) return nullptr;\n int in_order_index = findIndex(inorder, preorder[0]);\n TreeNode* root = new TreeNode(preorder[0]);\n\n vector<int> new_preorder_left(preorder.begin() + 1, preorder.end());\n vector<int> new_inorder_left(inorder.begin(), inorder.begin() + in_order_index);\n root->left = dfs(new_preorder_left, new_inorder_left);\n\n vector<int> new_preorder_right(preorder.begin() + in_order_index + 1, preorder.end());\n vector<int> new_inorder_right(inorder.begin() + in_order_index + 1, inorder.end());\n root->right = dfs(new_preorder_right, new_inorder_right);\n\n return root;\n }\n\n int findIndex(vector<int>& arr, int val) {\n for (int i = 0; i < arr.size(); i++) {\n if (arr[i] == val) return i;\n }\n return -1;\n }\n};\n",
"memory": "129150"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return dfs(preorder, inorder);\n }\n\n TreeNode* dfs(vector<int> preorder, vector<int> inorder) {\n if (preorder.size() == 0 || inorder.size() == 0) return nullptr;\n int in_order_index = findIndex(inorder, preorder[0]);\n TreeNode* root = new TreeNode(preorder[0]);\n\n vector<int> new_preorder_left(preorder.begin() + 1, preorder.end());\n vector<int> new_inorder_left(inorder.begin(), inorder.begin() + in_order_index);\n root->left = dfs(new_preorder_left, new_inorder_left);\n\n vector<int> new_preorder_right(preorder.begin() + in_order_index + 1, preorder.end());\n vector<int> new_inorder_right(inorder.begin() + in_order_index + 1, inorder.end());\n root->right = dfs(new_preorder_right, new_inorder_right);\n\n return root;\n }\n\n int findIndex(vector<int>& arr, int val) {\n for (int i = 0; i < arr.size(); i++) {\n if (arr[i] == val) return i;\n }\n return -1;\n }\n};",
"memory": "129150"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return dfs(preorder, inorder);\n }\n\n TreeNode* dfs(vector<int> preorder, vector<int> inorder) {\n if (preorder.size() == 0 || inorder.size() == 0) return nullptr;\n int in_order_index = find(inorder.begin(), inorder.end(), preorder[0]) - inorder.begin();\n TreeNode* root = new TreeNode(preorder[0]);\n\n vector<int> new_preorder_left(preorder.begin() + 1, preorder.end());\n vector<int> new_inorder_left(inorder.begin(), inorder.begin() + in_order_index);\n root->left = dfs(new_preorder_left, new_inorder_left);\n\n vector<int> new_preorder_right(preorder.begin() + in_order_index + 1, preorder.end());\n vector<int> new_inorder_right(inorder.begin() + in_order_index + 1, inorder.end());\n root->right = dfs(new_preorder_right, new_inorder_right);\n\n return root;\n }\n};\n",
"memory": "131461"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return dfs(preorder, inorder);\n }\n\n TreeNode* dfs(vector<int> preorder, vector<int> inorder) {\n if (preorder.empty() || inorder.empty()) return nullptr;\n int in_order_index = find(inorder.begin(), inorder.end(), preorder[0]) - inorder.begin();\n TreeNode* root = new TreeNode(preorder[0]);\n\n vector<int> new_preorder_left(preorder.begin() + 1, preorder.end());\n vector<int> new_inorder_left(inorder.begin(), inorder.begin() + in_order_index);\n root->left = dfs(new_preorder_left, new_inorder_left);\n\n vector<int> new_preorder_right(preorder.begin() + in_order_index + 1, preorder.end());\n vector<int> new_inorder_right(inorder.begin() + in_order_index + 1, inorder.end());\n root->right = dfs(new_preorder_right, new_inorder_right);\n\n return root;\n }\n};\n",
"memory": "133773"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return dfs(preorder, inorder);\n }\n\n TreeNode* dfs(vector<int> preorder, vector<int> inorder) {\n if (preorder.size() == 0 || inorder.size() == 0) return nullptr;\n int in_order_index = findIndex(inorder, preorder[0]);\n TreeNode* root = new TreeNode(preorder[0]);\n\n vector<int> new_preorder_left(preorder.begin() + 1, preorder.end());\n vector<int> new_inorder_left(inorder.begin(), inorder.begin() + in_order_index);\n root->left = dfs(new_preorder_left, new_inorder_left);\n\n vector<int> new_preorder_right(preorder.begin() + in_order_index + 1, preorder.end());\n vector<int> new_inorder_right(inorder.begin() + in_order_index + 1, inorder.end());\n root->right = dfs(new_preorder_right, new_inorder_right);\n\n return root;\n }\n\n int findIndex(vector<int>& arr, int val) {\n for (int i = 0; i < arr.size(); i++) {\n if (arr[i] == val) return i;\n }\n return -1;\n }\n};\n",
"memory": "136084"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n // Different Binary Trees.\n\n // Challenges:\n //. Given a number/node, how to decide if this is left child, or right\n // child, or get back to parent. . The answer is basicly coming from\n // inorder.\n\n // Use preorder to create child nodes.\n // Use inorder to decide if this is left or right child.\n // The main loop is around preorder.\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* root = nullptr;\n std::stack<TreeNode*> parents;\n int preorder_index = 0;\n root = buildSubTree(preorder, inorder, preorder_index, 0, inorder.size()-1, parents, true);\n return root;\n }\n\n TreeNode* buildSubTree(vector<int>& preorder, vector<int>& inorder,\n int& preorder_index, int inorder_x, int inorder_y,\n std::stack<TreeNode*> parents, bool is_left) {\n\n if(inorder_x > inorder_y) {\n return nullptr;\n }\n\n TreeNode* new_node = new TreeNode(preorder[preorder_index]);\n\n if (parents.size() == 0) {\n parents.push(new_node); \n } else if (is_left) {\n parents.top()->left = new_node;\n parents.push(new_node); \n } else {\n parents.top()->right = new_node;\n parents.push(new_node); \n }\n\n int target_index = inorder_x;\n while(inorder[target_index] != preorder[preorder_index] && target_index <= inorder_y) {\n target_index++;\n }\n preorder_index++;\n\n // left tree\n buildSubTree(preorder, inorder, preorder_index, inorder_x, target_index-1, parents, true);\n // right tree\n buildSubTree(preorder, inorder, preorder_index, target_index+1, inorder_y, parents,false);\n parents.pop();\n return new_node;\n \n }\n};",
"memory": "136084"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(TreeNode* &root,int ind, int i,int j,unordered_map<int,int> &mp,vector<int> preorder, int &k,vector<int> inorder){\n if(i>j) return;\n // cout<<inorder[ind]<<\" \"<<i<<\" \"<<j<<\" \"<<endl;\n TreeNode * temp = new TreeNode(inorder[ind]);\n temp->left=nullptr;\n temp->right=nullptr;\n root=temp;\n k++;\n if(k<preorder.size()) solve(root->left,mp[preorder[k]],i,ind-1,mp,preorder,k,inorder);\n if(k<preorder.size()) solve(root->right,mp[preorder[k]],ind+1,j,mp,preorder,k,inorder);\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* root=nullptr;\n TreeNode* temp=root;\n int ind=-1;\n unordered_map<int,int> mp;\n for(int i=0;i<inorder.size();i++){\n if(inorder[i]==preorder[0]) {\n ind=i;\n }\n mp[inorder[i]]=i;\n }\n int k=0;\n solve(root,ind,0,inorder.size()-1,mp,preorder,k,inorder);\n return root;\n }\n};",
"memory": "138395"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(TreeNode* &root,int ind, int i,int j,unordered_map<int,int> &mp,vector<int> preorder, int &k,vector<int> inorder){\n if(i>j) return;\n TreeNode * temp = new TreeNode(inorder[ind]);\n temp->left=nullptr;\n temp->right=nullptr;\n root=temp;\n k++;\n if(k<preorder.size()) solve(root->left,mp[preorder[k]],i,ind-1,mp,preorder,k,inorder);\n if(k<preorder.size()) solve(root->right,mp[preorder[k]],ind+1,j,mp,preorder,k,inorder);\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* root=nullptr;\n TreeNode* temp=root;\n int ind=-1;\n unordered_map<int,int> mp;\n for(int i=0;i<inorder.size();i++){\n if(inorder[i]==preorder[0]) {\n ind=i;\n }\n mp[inorder[i]]=i;\n }\n int k=0;\n solve(root,ind,0,inorder.size()-1,mp,preorder,k,inorder);\n return root;\n }\n};",
"memory": "138395"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n unordered_set<int> visited;\n TreeNode* rebuild(vector<int> pre, vector<int> in, int n, int s, int e){\n if(n > pre.size()){\n return NULL;\n }\n if(s > e){\n return NULL;\n }\n int start = pre[n];\n visited.insert(start);\n TreeNode* root = new TreeNode(start);\n int i = 0;\n for(; i<in.size(); i++){\n if(start == in[i])break;\n }\n if(i>0)\n root->left = rebuild(pre,in,n+1, s, i-1);\n while(n < pre.size() && visited.find(pre[n])!=visited.end()){\n n++;\n }\n if(i<in.size()-1 && visited.size()<pre.size())\n root->right = rebuild(pre,in,n,i+1,e);\n return root;\n }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* p = rebuild(preorder,inorder,0,0,preorder.size());\n return p;\n }\n};",
"memory": "140706"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n unordered_set<int> visited;\n TreeNode* rebuild(vector<int> pre, vector<int> in, int n, int s, int e){\n if(n > pre.size()){\n return NULL;\n }\n if(s > e){\n return NULL;\n }\n int start = pre[n];\n visited.insert(start);\n TreeNode* root = new TreeNode(start);\n int i = 0;\n for(; i<in.size(); i++){\n if(start == in[i])break;\n }\n if(i>0)\n root->left = rebuild(pre,in,n+1, s, i-1);\n while(n < pre.size() && visited.find(pre[n])!=visited.end()){\n n++;\n }\n if(i<in.size()-1 && visited.size()<pre.size())\n root->right = rebuild(pre,in,n,i+1,e);\n return root;\n }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* p = rebuild(preorder,inorder,0,0,preorder.size());\n return p;\n }\n};\n\nstatic int _performance_boost_ = []()\n{\n ios_base::sync_with_stdio(false),\n cin.tie(0),\n cout.tie(0);\n return 0;\n} ();\n",
"memory": "140706"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.size()==0) return nullptr;\n int rv=preorder[0];\n TreeNode* root=new TreeNode(rv);\n vector<int> Lp,Li;\n preorder.erase(preorder.begin());\n while((inorder.size())&&(inorder[0]!=rv)){\n Lp.push_back(preorder[0]);\n Li.push_back(inorder[0]);\n preorder.erase(preorder.begin());\n inorder.erase(inorder.begin());\n }\n inorder.erase(inorder.begin());\n root->left=buildTree(Lp,Li);\n root->right=buildTree(preorder,inorder);\n return root;\n }\n};",
"memory": "143018"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if (!preorder.size() || !inorder.size()) {\n return nullptr;\n }\n\n TreeNode* head = new TreeNode(preorder[0]);\n \n int size_left_tree;\n for (int i = 0; i < inorder.size(); i++) {\n if (inorder[i] == preorder[0]) {\n size_left_tree = i;\n break;\n }\n }\n vector<int> inorder_left;\n vector<int> inorder_right;\n vector<int> preorder_left;\n vector<int> preorder_right;\n \n for (int i = 0; i < inorder.size(); i++) {\n if (i < size_left_tree) {\n inorder_left.emplace_back(inorder[i]);\n } else if (i > size_left_tree) {\n inorder_right.emplace_back(inorder[i]);\n }\n if (i <= size_left_tree && i) {\n preorder_left.emplace_back(preorder[i]);\n } else if (i) {\n preorder_right.emplace_back(preorder[i]);\n }\n \n \n }\n\n head->left = buildTree(preorder_left, inorder_left);\n head->right = buildTree(preorder_right, inorder_right);\n return head;\n }\n};",
"memory": "145329"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.size() == 0 || inorder.size() == 0)\n return NULL;\n \n TreeNode* root = new TreeNode(preorder.front());\n\n vector<int> pre1, in1;\n vector<int> pre2, in2;\n int n = preorder.size();\n int i = 0;\n bool flag = false;\n while(i<n){\n if(inorder[i] == root -> val)\n flag = true;\n else if(!flag)\n in1.push_back(inorder[i]);\n else\n in2.push_back(inorder[i]);\n i++;\n }\n i = 1;\n flag = false;\n while(i<n){\n if(i > in1.size() && !flag)\n flag = true;\n \n if(!flag)\n pre1.push_back(preorder[i]);\n else\n pre2.push_back(preorder[i]);\n i++;\n }\n root -> left = buildTree(pre1, in1);\n root -> right = buildTree(pre2, in2);\n\n return root;\n }\n};",
"memory": "147640"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* solve(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.size() ==0 || inorder.size() == 0) {\n return nullptr;\n }\n auto root = new TreeNode(preorder[0]);\n vector<int> inorderLeft, inorderRight;\n bool swtch = false;\n for(int i = 0; i < inorder.size(); i++) {\n if(inorder[i] == preorder[0]) {\n swtch = true;\n continue;\n }\n \n if(swtch) {\n inorderRight.push_back(inorder[i]);\n } else {\n inorderLeft.push_back(inorder[i]);\n }\n }\n vector<int> preoderLeft, preoderRight;\n int i = 1;\n for(; i < inorderLeft.size() + 1; i++) {\n preoderLeft.push_back(preorder[i]);\n }\n \n for(; i < preorder.size(); i++) {\n preoderRight.push_back(preorder[i]);\n }\n //cout << preoderLeft.size() << \" \" << inorderLeft.size() << endl;\n root->left = solve(preoderLeft, inorderLeft);\n //cout << preoderRight.size() << \" \" << inorderRight.size() << endl;\n root->right = solve(preoderRight, inorderRight);\n return root;\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return solve(preorder, inorder);\n }\n};",
"memory": "149951"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if (preorder.size() == 1) {\n return new TreeNode(preorder[0]);\n }\n if (preorder.size() == 0) {\n return nullptr;\n }\n\n TreeNode* root = new TreeNode(preorder[0]);\n\n vector<int> leftInorder = getLeftInorder(root->val, inorder);\n vector<int> leftPreorder = getLeftPreorder(leftInorder.size(), preorder);\n vector<int> rightInorder = getRightInorder(root->val, inorder);\n vector<int> rightPreorder = getRightPreorder(leftPreorder.size() + 1, preorder);\n root->left = buildTree(leftPreorder, leftInorder);\n root->right = buildTree(rightPreorder, rightInorder);\n\n return root;\n }\n vector<int> getLeftInorder(int parentVal, vector<int>& inorder) {\n vector<int> leftInorder;\n for (int i = 0; i < inorder.size(); i++) {\n if (inorder[i] == parentVal) {\n break;\n }\n leftInorder.push_back(inorder.at(i));\n }\n\n return leftInorder;\n }\n vector<int> getLeftPreorder(int len, vector<int>& preorder) {\n vector<int> leftPreorder;\n for (int i = 1; i < len + 1; i++) {\n leftPreorder.push_back(preorder[i]);\n }\n\n return leftPreorder;\n }\n vector<int> getRightInorder(int parentVal, vector<int>& inorder) {\n bool seenParent = false;\n vector<int> rightInorder;\n for (int i = 0; i < inorder.size(); i++) {\n if (seenParent) {\n rightInorder.push_back(inorder[i]);\n }\n else {\n seenParent = inorder[i] == parentVal;\n }\n }\n\n return rightInorder;\n }\n vector<int> getRightPreorder(int start, vector<int>& preorder) {\n vector<int> rightPreorder;\n for (int i = start; i < preorder.size(); i++) {\n rightPreorder.push_back(preorder[i]);\n }\n\n return rightPreorder;\n }\n\n void printVector(vector<int> vec) {\n for (int i = 0; i < vec.size(); i++) {\n cout << vec[i] << \" \";\n }\n cout << endl;\n }\n};",
"memory": "152263"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if( preorder.empty() ) return nullptr;\n TreeNode* root = new TreeNode(preorder[0]);\n vector<int> p1, p2, i1, i2;\n bool findRoot = false;\n for( int n : inorder ){\n if( n==preorder[0] ){\n findRoot = true;\n } else {\n if( findRoot ){\n i2.push_back(n);\n } else {\n i1.push_back(n);\n }\n }\n }\n for( int n : preorder ){\n if( n != preorder[0] ){\n if( p1.size() < i1.size() ){\n p1.push_back(n);\n } else {\n p2.push_back(n);\n }\n }\n }\n root->left = buildTree(p1,i1);\n root->right = buildTree(p2,i2);\n return root;\n\n }\n};",
"memory": "154574"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.size()==0) return NULL;\n int pos=0,n=preorder.size();\n while(inorder[pos]!=preorder[0])\n pos++;\n TreeNode *root=new TreeNode(preorder[0]);\n vector<int> preArr,inArr;\n for(int i=1;i<=pos;i++)\n preArr.push_back(preorder[i]);\n for(int i=0;i<pos;i++)\n inArr.push_back(inorder[i]);\n root->left=buildTree(preArr,inArr);\n preArr.clear();\n inArr.clear();\n for(int i=pos+1;i<n;i++)\n preArr.push_back(preorder[i]);\n for(int i=pos+1;i<n;i++)\n inArr.push_back(inorder[i]);\n root->right=buildTree(preArr,inArr);\n return root;\n }\n};",
"memory": "156885"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.empty())\n return NULL;\n TreeNode* root = new TreeNode(preorder[0]);\n vector<int>left_inorder;\n vector<int>right_inorder;\n int flag = 0;\n int count_left = 0;\n for(auto &x:inorder){\n if(x==preorder[0]){\n flag=1;\n continue;\n }\n else if(flag==0){\n left_inorder.push_back(x);\n count_left++;\n }\n else if(flag==1){\n right_inorder.push_back(x);\n }\n }\n vector<int>left_preorder;\n vector<int>right_preorder;\n for(int i=1;i<=count_left;i++)\n left_preorder.push_back(preorder[i]);\n for(int i=count_left+1;i<preorder.size();i++)\n right_preorder.push_back(preorder[i]);\n root->left = buildTree(left_preorder,left_inorder);\n root->right = buildTree(right_preorder,right_inorder);\n return root;\n }\n};",
"memory": "156885"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.size()==0)\n return nullptr;\n \n TreeNode *root = new TreeNode(preorder[0]);\n int mid,i;\n for(i=0;i<inorder.size();i++){\n if(inorder[i]==preorder[0]){\n mid=i;\n break;\n }\n }\n \n \n vector<int> leftPreorder,leftInorder,rightPreorder,rightInorder;\n \n for(i=1;i<mid+1;i++)\n leftPreorder.push_back(preorder[i]);\n for(i=0;i<mid;i++)\n leftInorder.push_back(inorder[i]);\n \n for(i=mid+1;i<preorder.size();i++)\n rightPreorder.push_back(preorder[i]);\n for(i=mid+1;i<inorder.size();i++)\n rightInorder.push_back(inorder[i]);\n \n root->left = buildTree(leftPreorder,leftInorder);\n root->right = buildTree(rightPreorder,rightInorder);\n \n return root;\n }\n};",
"memory": "159196"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.empty())\n return NULL;\n TreeNode* root = new TreeNode(preorder[0]);\n vector<int>left_inorder;\n vector<int>right_inorder;\n int flag = 0;\n int count_left = 0;\n for(auto &x:inorder){\n if(x==preorder[0]){\n flag=1;\n continue;\n }\n else if(flag==0){\n left_inorder.push_back(x);\n count_left++;\n }\n else if(flag==1){\n right_inorder.push_back(x);\n }\n }\n vector<int>left_preorder;\n vector<int>right_preorder;\n for(int i=1;i<=count_left;i++)\n left_preorder.push_back(preorder[i]);\n for(int i=count_left+1;i<preorder.size();i++)\n right_preorder.push_back(preorder[i]);\n root->left = buildTree(left_preorder,left_inorder);\n root->right = buildTree(right_preorder,right_inorder);\n return root;\n }\n};",
"memory": "161508"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.size() == 0) return nullptr;\n TreeNode *root = new TreeNode(preorder[0]);\n vector<int> leftpre, rightpre, leftin, rightin;\n auto it = find(inorder.begin(), inorder.end(), preorder[0]);\n copy(inorder.begin(), it, back_inserter(leftin));\n copy(it+1, inorder.end(), back_inserter(rightin));\n int size = distance(inorder.begin(), it);\n copy(preorder.begin()+1, preorder.begin()+size+1, back_inserter(leftpre));\n copy(preorder.begin()+size+1, preorder.end(), back_inserter(rightpre));\n root->left = buildTree(leftpre, leftin);\n root->right = buildTree(rightpre, rightin);\n\n return root;\n \n }\n};",
"memory": "161508"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int i,n=(int)preorder.size(),idInorder,idPreorder,rootVal;\n set<int> st;\n vector<int> preorderl,preorderr,inorderl,inorderr;\n TreeNode* root = new TreeNode();\n\n // cout<<preorder.size()<<\"pre sz\\n\";\n // cout<<inorder.size()<<\"in sz\\n\";\n\n if(preorder.empty()) return 0;\n\n rootVal=preorder[0];\n\n for(i=0;i<(int)inorder.size();i++){\n if(inorder[i]==rootVal) break;\n }\n idInorder=i;\n cout<<idInorder<<\"idInorder\\n\";\n\n for(i=idInorder+1;i<(int)inorder.size();i++){\n st.insert(inorder[i]);\n }\n\n for(i=1;i<n;i++){\n if(st.find(preorder[i])!=st.end()) break;\n }\n idPreorder=i-1;\n\n for(i=0;i<n;i++){\n if(i<idInorder) inorderl.push_back(inorder[i]);\n else if(i>idInorder) inorderr.push_back(inorder[i]);\n }\n for(i=1;i<n;i++){\n if(i<=idPreorder) preorderl.push_back(preorder[i]);\n else preorderr.push_back(preorder[i]);\n }\n\n root->val = inorder[idInorder];\n root->left = buildTree(preorderl,inorderl);\n root->right = buildTree(preorderr,inorderr);\n\n return root;\n\n }\n};",
"memory": "163819"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n\n \n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.size() == 0) return NULL;\n TreeNode* root = new TreeNode;\n int root_val = preorder[0];\n root->val = root_val;\n vector<vector<int>> left(2);\n vector<vector<int>> right(2);\n int k = 0;\n int left_size;\n int right_size;\n while(k<inorder.size() && inorder[k] != root_val){\n left[1].push_back(inorder[k]);\n k++;\n }\n left_size = k;\n k++;\n while(k<inorder.size()){\n right[1].push_back(inorder[k]);\n k++;\n }\n // right_size = preorder.size() - 1 - left_size;\n \n int p = 1;\n while(p<=left_size){\n left[0].push_back(preorder[p]);\n p++;\n }\n // p++;\n while(p<preorder.size()){\n right[0].push_back(preorder[p]);\n p++;\n }\n TreeNode* left_subtree = buildTree(left[0],left[1]);\n TreeNode* right_subtree = buildTree(right[0], right[1]);\n root->left = left_subtree;\n root->right = right_subtree;\n return root;\n \n }\n};",
"memory": "163819"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> subVector(vector<int> vec, int start, int end){\n if (end > vec.size() || start > end || start < 0){\n return {};\n }\n return vector<int>(vec.begin()+start, vec.begin()+end+1);\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if (preorder.empty() || inorder.empty())\n return nullptr;\n int rootValue = preorder[0];\n TreeNode* root = new TreeNode(rootValue);\n // finding the root in the inorder array\n auto it = find(inorder.begin(), inorder.end(), rootValue);\n int i = distance(inorder.begin(), it);\n\n vector<int> leftInOrder = subVector(inorder, 0, i-1);\n vector<int> rightInOrder = subVector(inorder, i+1, inorder.size()-1);\n vector<int> leftPreOrder = subVector(preorder, 1, leftInOrder.size());\n vector<int> rightPreOrder = subVector(preorder, 1 + leftInOrder.size(),\n leftInOrder.size()+rightInOrder.size());\n root->left = buildTree(leftPreOrder, leftInOrder);\n root->right = buildTree(rightPreOrder, rightInOrder);\n return root;\n }\n};",
"memory": "166130"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> subVector(vector<int> vec, int start, int end){\n if (end > vec.size() || start > end || start < 0){\n return {};\n }\n return vector<int>(vec.begin()+start, vec.begin()+end+1);\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if (preorder.empty() || inorder.empty())\n return nullptr;\n int rootValue = preorder[0];\n TreeNode* root = new TreeNode(rootValue);\n // finding the root in the inorder array\n auto it = find(inorder.begin(), inorder.end(), rootValue);\n int i = distance(inorder.begin(), it);\n\n vector<int> leftInOrder = subVector(inorder, 0, i-1);\n vector<int> rightInOrder = subVector(inorder, i+1, inorder.size()-1);\n vector<int> leftPreOrder = subVector(preorder, 1, leftInOrder.size());\n vector<int> rightPreOrder = subVector(preorder, 1 + leftInOrder.size(),\n leftInOrder.size()+rightInOrder.size());\n root->left = buildTree(leftPreOrder, leftInOrder);\n root->right = buildTree(rightPreOrder, rightInOrder);\n return root;\n }\n};",
"memory": "168441"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> subVector(vector<int> vec, int start, int end){\n if (end > vec.size() || start > end || start < 0){\n return {};\n }\n return vector<int>(vec.begin()+start, vec.begin()+end+1);\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if (preorder.empty() || inorder.empty())\n return nullptr;\n int rootValue = preorder[0];\n TreeNode* root = new TreeNode(rootValue);\n // finding the root in the inorder array\n auto it = find(inorder.begin(), inorder.end(), rootValue);\n int i = distance(inorder.begin(), it);\n\n vector<int> leftInOrder = subVector(inorder, 0, i-1);\n vector<int> rightInOrder = subVector(inorder, i+1, inorder.size()-1);\n vector<int> leftPreOrder = subVector(preorder, 1, leftInOrder.size());\n vector<int> rightPreOrder = subVector(preorder, 1 + leftInOrder.size(),\n leftInOrder.size()+rightInOrder.size());\n root->left = buildTree(leftPreOrder, leftInOrder);\n root->right = buildTree(rightPreOrder, rightInOrder);\n return root;\n }\n};",
"memory": "168441"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int position(int element,vector<int> inorder){\n for(int i=0;i<inorder.size();i++){\n if(inorder[i] == element) return i;\n }\n return -1;\n }\n TreeNode* solve(int &i,int min,int max,vector<int> &preorder,vector<int> inorder){\n if(i>=preorder.size() || max<min) return NULL;\n TreeNode* node = new TreeNode(preorder[i++]);\n int index = position(node->val,inorder);\n\n node->left = solve(i,min,index-1,preorder,inorder);\n node->right = solve(i,index+1,max,preorder,inorder);\n\n return node;\n }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int i = 0;\n return solve(i,0,preorder.size()-1,preorder,inorder); \n }\n};",
"memory": "170753"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\nprivate:\n int position(int element, vector<int> inorder) {\n for (int i = 0; i < inorder.size(); i++) {\n if (element == inorder[i])\n return i;\n }\n return -1;\n }\n\n TreeNode* solve(vector<int> preorder, vector<int>& inorder,\n int& preorderIndex, int inorderStart, int inorderEnd) {\n if (preorderIndex > inorder.size() || inorderStart > inorderEnd) {\n return NULL;\n }\n\n int element = preorder[preorderIndex++];\n TreeNode* root = new TreeNode(element);\n int pos = position(element, inorder);\n\n root->left =\n solve(preorder, inorder, preorderIndex, inorderStart, pos - 1);\n root->right =\n solve(preorder, inorder, preorderIndex, pos + 1, inorderEnd);\n\n return root;\n }\n\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int preorderIndex = 0;\n int n = inorder.size();\n TreeNode* ans = solve(preorder, inorder, preorderIndex, 0, n - 1);\n return ans;\n }\n};",
"memory": "173064"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n int searchInOrder(vector<int>inorder, int element){\n for(int i=0;i<inorder.size();i++){\n if(inorder[i]==element)\n return i;\n }\n\n return -1;\n }\n\n TreeNode* solve(vector<int>& preorder,int &preIndex, vector<int>inorder, int inStart, int inEnd, int size){\\\n if(preIndex>=size || inStart>inEnd)\n return NULL;\n\n // Solve 1 case \n int element=preorder[preIndex];\n preIndex++;\n\n TreeNode* root=new TreeNode(element);\n\n int position=searchInOrder(inorder,element);\n\n root->left=solve(preorder,preIndex,inorder,inStart,position-1,size);\n\n root->right=solve(preorder,preIndex,inorder,position+1,inEnd,size);\n\n return root;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int size=preorder.size();\n int preIndex=0;\n int inStart=0;\n int inEnd=inorder.size()-1;\n\n TreeNode* root=solve(preorder,preIndex,inorder,inStart,inEnd,size);\n return root;\n }\n};",
"memory": "173064"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n \n int n = preorder.size();\n\n // Base Case\n if (n == 0) {\n return nullptr;\n }\n\n int root_val = preorder[0];\n int root_val_inorder_index = findIndex(root_val, inorder);\n\n int left_subtree_size = root_val_inorder_index;\n int right_subtree_size = n - root_val_inorder_index - 1;\n \n vector<int> left_subtree_preorder;\n vector<int> left_subtree_inorder;\n for (int i = 1; i < left_subtree_size + 1; ++i) {\n left_subtree_preorder.push_back(preorder[i]);\n }\n for (int i = 0; i < left_subtree_size; ++i) {\n left_subtree_inorder.push_back(inorder[i]);\n }\n\n vector<int> right_subtree_preorder;\n vector<int> right_subtree_inorder;\n for (int i = 1 + left_subtree_size; i < 1 + left_subtree_size + right_subtree_size; ++i) {\n right_subtree_preorder.push_back(preorder[i]);\n }\n for (int i = root_val_inorder_index + 1; i < root_val_inorder_index + 1 + right_subtree_size; ++i) {\n right_subtree_inorder.push_back(inorder[i]);\n }\n\n TreeNode* root = new TreeNode(root_val);\n root->left = this->buildTree(left_subtree_preorder, left_subtree_inorder);\n root->right = this->buildTree(right_subtree_preorder, right_subtree_inorder);\n\n return root;\n }\n\n int findIndex(int x, vector<int> tree_vector) {\n for (int i = 0; i < tree_vector.size(); ++i) {\n if (tree_vector[i] == x) {\n return i;\n }\n }\n return -1;\n }\n\n};",
"memory": "175375"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n \n int n = preorder.size();\n\n // Base Case\n if (n == 0) {\n return nullptr;\n }\n\n int root_val = preorder[0];\n int root_val_inorder_index = findIndex(root_val, inorder);\n\n int left_subtree_size = root_val_inorder_index;\n int right_subtree_size = n - root_val_inorder_index - 1;\n \n vector<int> left_subtree_preorder;\n vector<int> left_subtree_inorder;\n for (int i = 1; i < left_subtree_size + 1; ++i) {\n left_subtree_preorder.push_back(preorder[i]);\n }\n for (int i = 0; i < left_subtree_size; ++i) {\n left_subtree_inorder.push_back(inorder[i]);\n }\n\n vector<int> right_subtree_preorder;\n vector<int> right_subtree_inorder;\n for (int i = 1 + left_subtree_size; i < 1 + left_subtree_size + right_subtree_size; ++i) {\n right_subtree_preorder.push_back(preorder[i]);\n }\n for (int i = root_val_inorder_index + 1; i < root_val_inorder_index + 1 + right_subtree_size; ++i) {\n right_subtree_inorder.push_back(inorder[i]);\n }\n\n TreeNode* root = new TreeNode(root_val);\n root->left = this->buildTree(left_subtree_preorder, left_subtree_inorder);\n root->right = this->buildTree(right_subtree_preorder, right_subtree_inorder);\n\n return root;\n }\n\n int findIndex(int x, vector<int> tree_vector) {\n for (int i = 0; i < tree_vector.size(); ++i) {\n if (tree_vector[i] == x) {\n return i;\n }\n }\n return -1;\n }\n\n};",
"memory": "175375"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * }; \n */\nclass Solution {\npublic:\n int index(vector<int> list, int elm){\n // auto it = find(list.begin(),list.end(), elm);\n // int ind = distance(list.begin(), it);\n // return ind;\n int i;\n for(i = 0; i < list.size(); i++){\n if(list[i] == elm){\n break;\n }\n }\n return i;\n }\n\n vector<int> sliced(vector<int>& v, int start, int end){\n vector<int> res;\n for(int i = start; i < end; i++){\n res.push_back(v[i]);\n }\n return res;\n }\n\n TreeNode* build(vector<int> preorder, vector<int> inorder){\n if(preorder.empty()){\n return nullptr;\n }\n\n TreeNode* root = new TreeNode(preorder[0]);\n int mid = index(inorder, preorder[0]);\n root->left = build(sliced(preorder,1, mid+1) , sliced(inorder, 0, mid));\n root->right = build(sliced(preorder,mid + 1, preorder.size()) , sliced(inorder, mid+1, inorder.size()));\n return root;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return build(preorder, inorder);\n }\n};\n\n // build(preorder, inorder, index, 0, inorder.size() - 1);",
"memory": "177686"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * }; \n */\nclass Solution {\npublic:\n int index(vector<int> list, int elm){\n // auto it = find(list.begin(),list.end(), elm);\n // int ind = distance(list.begin(), it);\n // return ind;\n int i;\n for(i = 0; i < list.size(); i++){\n if(list[i] == elm){\n break;\n }\n }\n return i;\n }\n\n vector<int> sliced(vector<int>& v, int start, int end){\n vector<int> res;\n for(int i = start; i < end; i++){\n res.push_back(v[i]);\n }\n return res;\n }\n\n TreeNode* buildTree(vector<int> preorder, vector<int> inorder) {\n if(preorder.empty()){\n return nullptr;\n }\n\n TreeNode* root = new TreeNode(preorder[0]);\n int mid = index(inorder, preorder[0]);\n root->left = buildTree(sliced(preorder,1, mid+1) , sliced(inorder, 0, mid));\n root->right = buildTree(sliced(preorder,mid + 1, preorder.size()) , sliced(inorder, mid+1, inorder.size()));\n return root;\n }\n};\n\n // build(preorder, inorder, index, 0, inorder.size() - 1);",
"memory": "177686"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n TreeNode* construct(vector<int>preorder, vector<int>inorder){\n if(preorder.size()==0){\n return nullptr;\n }\n else if(preorder.size()==1){\n TreeNode *root = new TreeNode(preorder[0]);\n return root;\n }\n else if(preorder.size()==2){\n TreeNode *root = new TreeNode(preorder[0]);\n TreeNode *children = new TreeNode(preorder[1]);\n if(preorder[1]==inorder[1]){\n root->right = children; \n }\n else{\n root->left = children;\n }\n return root;\n }\n // else if(preorder.size()==3){\n // TreeNode* root = new TreeNode(preorder[0]);\n // root->left = new TreeNode(preorder[1]);\n // root->right = new TreeNode(preorder[2]);\n // return root;\n // }\n\n int rootVal = preorder[0];\n vector<int>leftIn, leftPre, rightIn, rightPre;\n\n int findR = 0;\n for(int i = 0 ; i < inorder.size() ; i++){\n if(rootVal == inorder[i]){\n findR = 1;\n continue;\n }\n if(findR){\n rightIn.push_back(inorder[i]);\n }\n else{\n leftIn.push_back(inorder[i]);\n }\n\n }\n int left = leftIn.size();\n for(int i = 1 ; i < preorder.size() ; i++){\n if(i<=left){\n leftPre.push_back(preorder[i]);\n } \n else{\n rightPre.push_back(preorder[i]);\n }\n }\n\n TreeNode* root = new TreeNode(rootVal);\n root->left = construct(leftPre, leftIn);\n root->right = construct(rightPre, rightIn);\n\n return root;\n }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n\n return construct(preorder,inorder);\n \n }\n};",
"memory": "179998"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n TreeNode* construct(vector<int>preorder, vector<int>inorder){\n if(preorder.size()==0){\n return nullptr;\n }\n else if(preorder.size()==1){\n TreeNode *root = new TreeNode(preorder[0]);\n return root;\n }\n else if(preorder.size()==2){\n TreeNode *root = new TreeNode(preorder[0]);\n TreeNode *children = new TreeNode(preorder[1]);\n if(preorder[1]==inorder[1]){\n root->right = children; \n }\n else{\n root->left = children;\n }\n return root;\n }\n // else if(preorder.size()==3){\n // TreeNode* root = new TreeNode(preorder[0]);\n // root->left = new TreeNode(preorder[1]);\n // root->right = new TreeNode(preorder[2]);\n // return root;\n // }\n\n int rootVal = preorder[0];\n vector<int>leftIn, leftPre, rightIn, rightPre;\n\n int findR = 0;\n for(int i = 0 ; i < inorder.size() ; i++){\n if(rootVal == inorder[i]){\n findR = 1;\n continue;\n }\n if(findR){\n rightIn.push_back(inorder[i]);\n }\n else{\n leftIn.push_back(inorder[i]);\n }\n\n }\n int left = leftIn.size();\n for(int i = 1 ; i < preorder.size() ; i++){\n if(i<=left){\n leftPre.push_back(preorder[i]);\n } \n else{\n rightPre.push_back(preorder[i]);\n }\n }\n\n TreeNode* root = new TreeNode(rootVal);\n root->left = construct(leftPre, leftIn);\n root->right = construct(rightPre, rightIn);\n\n return root;\n }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n\n return construct(preorder,inorder);\n \n }\n};",
"memory": "179998"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* cons(vector<int> preorder,vector<int> inorder){\n if(preorder.size() == 0){return NULL;}\n int root_val = preorder[0];\n TreeNode * a = new TreeNode(root_val);\n int index;\n vector<int> i1;\n vector<int> i2;\n for(int i =0;i<preorder.size();i++){\n if(inorder[i] == root_val){index = i;break;}\n }\n for(int i =0;i<inorder.size();i++){\n if(i<index){i1.push_back(inorder[i]);}\n else if( i>index){\n i2.push_back(inorder[i]);\n }\n }\n vector<int> p1;\n vector<int>p2;\n for(int i =1;i<preorder.size();i++){\n if(i<=index){p1.push_back(preorder[i]);}\n else{\n p2.push_back(preorder[i]);\n }\n }\n \n \n a->left = cons(p1,i1);\n \n a->right = cons(p2,i2);\n\n \n return a;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* a = cons(preorder,inorder);\n return a;\n }\n};",
"memory": "182309"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n TreeNode* helper(vector<int> preorder, vector<int> inorder)\n {\n if (preorder.size() == 0) return nullptr;\n\n TreeNode* root = new TreeNode(preorder[0]);\n int idx = -1;\n\n for (int i = 0; i < inorder.size(); i++)\n {\n if (root->val == inorder[i])\n {\n idx = i;\n break;\n }\n }\n\n vector<int> lpreorder;\n vector<int> linorder;\n\n for (int i = 0; i < idx; i++)\n {\n lpreorder.push_back(preorder[i+1]);\n linorder.push_back(inorder[i]);\n }\n\n vector<int> rpreorder;\n vector<int> rinorder;\n\n for (int i = idx+1; i < preorder.size(); i++)\n {\n rpreorder.push_back(preorder[i]);\n rinorder.push_back(inorder[i]);\n }\n\n TreeNode* left = helper(lpreorder, linorder); \n TreeNode* right = helper(rpreorder, rinorder); \n root->left = left;\n root->right = right;\n\n return root;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* root = helper(preorder, inorder);\n return root;\n }\n};",
"memory": "184620"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildCase(vector<int> preorder, vector<int> inorder){\n if(preorder.empty()||inorder.empty()) return NULL;\n TreeNode* root = new TreeNode(preorder[0]);\n int count=0,index=0;\n vector<int> leftPreorder,rightPreorder,leftInorder,rightInorder;\n for(int i=0;i<inorder.size();i++){\n if(inorder[i]==root->val){\n count =1;\n index=i;\n continue;\n }\n if(count==0){\n leftInorder.push_back(inorder[i]);\n }\n else{\n rightInorder.push_back(inorder[i]);\n }\n }\n for(int i=1;i<preorder.size();i++){\n if(i<=index){\n leftPreorder.push_back(preorder[i]);\n }\n else{\n rightPreorder.push_back(preorder[i]);\n }\n }\n root->left = buildCase(leftPreorder,leftInorder);\n root->right = buildCase(rightPreorder,rightInorder);\n return root;\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* root = buildCase(preorder,inorder);\n return root;\n \n }\n};",
"memory": "186931"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n TreeNode* helper(vector<int> preorder, vector<int> inorder)\n {\n if (preorder.size() == 0) return nullptr;\n\n TreeNode* root = new TreeNode(preorder[0]);\n int idx = -1;\n\n for (int i = 0; i < inorder.size(); i++)\n {\n if (root->val == inorder[i])\n {\n idx = i;\n break;\n }\n }\n\n vector<int> lpreorder;\n vector<int> linorder;\n\n for (int i = 0; i < idx; i++)\n {\n lpreorder.push_back(preorder[i+1]);\n linorder.push_back(inorder[i]);\n }\n\n vector<int> rpreorder;\n vector<int> rinorder;\n\n for (int i = idx+1; i < preorder.size(); i++)\n {\n rpreorder.push_back(preorder[i]);\n rinorder.push_back(inorder[i]);\n }\n\n TreeNode* left = helper(lpreorder, linorder); \n TreeNode* right = helper(rpreorder, rinorder); \n root->left = left;\n root->right = right;\n\n return root;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* root = helper(preorder, inorder);\n return root;\n }\n};",
"memory": "189243"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return buildTreeAux(preorder,inorder);\n }\n TreeNode* buildTreeAux(vector<int> preorder, vector<int> inorder)\n {\n if( preorder.size() == 0 ) return NULL;\n int rootVal = preorder[0];\n vector<int> leftInorder,rightInorder;\n int i = 0;\n for(; i < preorder.size() ; i++ ){\n if( inorder[i] == rootVal )\n break;\n leftInorder.push_back( inorder[i] );\n }\n i++;\n for(; i < preorder.size() ; i++ ){\n rightInorder.push_back( inorder[i] );\n }\n\n vector<int> leftPreorder,rightPreorder;\n for(int i = 0 ; i < leftInorder.size();i++)\n leftPreorder.push_back(preorder[i+1]);\n for(int i = 0 ; i < rightInorder.size() ; i++ )\n rightPreorder.push_back(preorder[leftPreorder.size()+i+1]);\n TreeNode* root = new TreeNode( rootVal );\n root->left = buildTreeAux(leftPreorder,leftInorder);\n root->right = buildTreeAux(rightPreorder,rightInorder);\n return root;\n }\n};",
"memory": "191554"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* build(vector<int> preorder, int prelo , int prehi, vector<int> inorder, int inlo, int inhi){\n if(prelo > prehi) return NULL;\n TreeNode* root = new TreeNode(preorder[prelo]);\n if(prelo == prehi) return root;\n int i = inlo;\n while(i<=inhi){\n if(inorder[i] == preorder[prelo]) break;\n i++;\n }\n int LeftCount = i - inlo;\n int RightCount = inhi - i;\n root->left = build(preorder , prelo+1, prelo+LeftCount, inorder , inlo , i-1);\n root->right = build(preorder,prelo+LeftCount+1,prehi,inorder,i+1,inhi);\n return root;\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int n = preorder.size();\n return build(preorder,0,n-1,inorder,0,n-1);\n }\n};",
"memory": "193865"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\nTreeNode*build(vector<int>pre,int prelo,int prehi,vector<int>in,int inlo,int inhi){\n if(prelo>prehi) return NULL;\n TreeNode*root=new TreeNode(pre[prelo]);\n if(prelo==prehi) return root;\n int i=inlo;\n while(i<inhi){\n if(in[i]==pre[prelo]) break;\n i++;\n }\n int leftcount=i-inlo;\n int rightcount=inhi-i;\n root->left=build(pre,prelo+1,prelo+leftcount,in,inlo,i-1);\n root->right=build(pre,prelo+leftcount+1,prehi,in,i+1,inhi);\n return root;\n\n}\n TreeNode* buildTree(vector<int>& pre, vector<int>& in) {\n int n=pre.size();\n return build(pre,0,n-1,in,0,n-1);\n \n }\n};",
"memory": "196176"
} |
105 | <p>Given two integer arrays <code>preorder</code> and <code>inorder</code> where <code>preorder</code> is the preorder traversal of a binary tree and <code>inorder</code> is the inorder traversal of the same tree, construct and return <em>the binary tree</em>.</p>
<p> </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> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
<strong>Output:</strong> [3,9,20,null,null,15,7]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> preorder = [-1], inorder = [-1]
<strong>Output:</strong> [-1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= preorder.length <= 3000</code></li>
<li><code>inorder.length == preorder.length</code></li>
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
<li><code>preorder</code> and <code>inorder</code> consist of <strong>unique</strong> values.</li>
<li>Each value of <code>inorder</code> also appears in <code>preorder</code>.</li>
<li><code>preorder</code> is <strong>guaranteed</strong> to be the preorder traversal of the tree.</li>
<li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int idx = 0;\n TreeNode* preorderToInordertree(vector<int> preorder, vector<int> inorder, int start, int end){\n if(start>end){\n return NULL;\n }\n\n int key = preorder[idx];\n idx++;\n TreeNode* node = new TreeNode(key);\n \n if(start==end){\n return node;\n }\n\n int pos;\n\n for(int i=start; i<=end; i++){\n if(inorder[i] == key){\n pos=i;\n break;\n }\n }\n\n node->left = preorderToInordertree(preorder, inorder, start, pos-1);\n node->right = preorderToInordertree(preorder, inorder, pos+1, end);\n\n return node;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if(preorder.size()==0){\n return NULL;\n }\n\n TreeNode* ans = preorderToInordertree(preorder, inorder, 0, preorder.size()-1);\n\n return ans;\n }\n};",
"memory": "198488"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid make(vector<int>& postorder, vector<int>& inorder, TreeNode*& root, int &i, int start, int end) {\n if (start > end) return; \n int inIndex = start;\n while (inIndex <= end && inorder[inIndex] != postorder[i]) inIndex++;\n root = new TreeNode(postorder[i]);\n i--;\n make(postorder, inorder, root->right, i, inIndex + 1, end);\n make(postorder, inorder, root->left, i, start, inIndex - 1);\n}\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (postorder.empty() || inorder.empty()) return nullptr;\n TreeNode* root = nullptr;\n int i = postorder.size() - 1; \n make(postorder, inorder, root, i, 0, inorder.size() - 1);\n return root;\n }\n};",
"memory": "26920"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n TreeNode* root = new TreeNode();\n root->val = postorder[postorder.size()-1];\n createTree(root, inorder, postorder, 0, postorder.size()-1, postorder.size()-1);\n return root;\n }\n int createTree(TreeNode* root, vector<int>& inorder, vector<int>& postorder, int start, int end, int c_index) {\n int root_index = findIndex(inorder, root->val);\n bool has_right = (root_index < end);\n bool has_left = (root_index > start);\n\n if(has_right) {\n root->right = new TreeNode();\n root->right->val = postorder[--c_index];\n c_index = createTree(root->right, inorder, postorder, root_index + 1, end, c_index);\n }\n if(has_left) {\n root->left = new TreeNode();\n root->left->val = postorder[--c_index];\n c_index = createTree(root->left, inorder, postorder, start, root_index - 1, c_index);\n }\n return c_index;\n }\n int findIndex(vector<int>& inorder, int ele) {\n int len = inorder.size();\n for(int i = 0; i < len; ++i) {\n if(ele == inorder[i])\n return i;\n }\n return -1;\n }\n};",
"memory": "26920"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* CreatetheTree(vector<int>& inorder,int instart,int inend, vector<int>& postorder,int poststart,int postend,unordered_map<int,int>& mpp){\n if (poststart > postend)\n {\n return nullptr;\n }\n\n int startNode = postorder[postend];\n\n TreeNode* temp = new TreeNode(startNode);\n\n int splitindex = mpp[startNode];\n\n int sizeofleftsubtree = splitindex-instart;\n temp->left = CreatetheTree(inorder,instart,splitindex-1,postorder,poststart,poststart+sizeofleftsubtree-1,mpp);\n temp->right = CreatetheTree(inorder,splitindex+1,inend,postorder,poststart+sizeofleftsubtree,postend-1,mpp);\n\n return temp;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n unordered_map<int,int> mpp;\n for (int i = 0; i < inorder.size(); i++)\n {\n mpp[inorder[i]] = i;\n }\n return CreatetheTree(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1,mpp);\n }\n};",
"memory": "28161"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<int,int> map;\n TreeNode* helper(vector<int>& inorder,vector<int>& postorder,int inStart,int inEnd,int& postIndex){\n if(inStart>inEnd)return nullptr;\n\n int rootVal=postorder[postIndex--];\n TreeNode* root=new TreeNode(rootVal);\n\n int inRoot=map[rootVal];\n root->right=helper(inorder,postorder,inRoot+1,inEnd,postIndex);\n root->left=helper(inorder,postorder,inStart,inRoot-1,postIndex);\n\n return root;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n=inorder.size();\n for(int i=0;i<n;i++)\n map[inorder[i]]=i;\n int postIndex=n-1;\n return helper(inorder,postorder,0,n-1,postIndex);\n }\n};",
"memory": "28161"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n std::unordered_map<int,int> numIdx;\n for(int i = inorder.size()-1;i>=0;i--){\n numIdx[inorder[i]] = i;\n }\n std::function<TreeNode*(const std::vector<int>&,int,int,const std::vector<int>&,int,int)> build = [&](const auto& inorder,int l1,int r1,const auto& post,int l2,int r2){\n TreeNode *root = nullptr;\n if(l1<=r1 && l2<=r2){\n auto val = postorder[r2];\n root = new TreeNode(val);\n int i = numIdx[val];\n root->left = build(inorder,l1,i-1,postorder,l2,l2+i-l1-1);\n root->right = build(inorder,i+1,r1,postorder,l2+i-l1,r2-1);\n }\n return root;\n };\n return build(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1);\n }\n};",
"memory": "29403"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if(inorder.size()==0) return NULL;\n int root = postorder[postorder.size()-1];\n postorder.pop_back();\n int index = -1;\n for(int i = 0; i < inorder.size(); i++){\n if(inorder[i] == root){\n index = i;\n break;\n }\n }\n int l = inorder.size() - index - 1;\n vector<int> right_inorder,right_postorder;\n for(int i = 0; i < l; i++){\n right_inorder.push_back(inorder[inorder.size()-1]);\n inorder.pop_back();\n right_postorder.push_back(postorder[postorder.size()-1]);\n postorder.pop_back();\n }\n reverse(right_inorder.begin(),right_inorder.end());\n reverse(right_postorder.begin(),right_postorder.end());\n inorder.pop_back();\n TreeNode* head = new TreeNode(root);\n head -> left = buildTree(inorder,postorder);\n head -> right = buildTree(right_inorder,right_postorder);\n return head;\n }\n};",
"memory": "29403"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n TreeNode* construct(int inStart, int inEnd, int postStart, int postEnd,\n vector<int>& inorder, vector<int>& postorder, vector<int>& inOrderIdx) {\n if (inStart > inEnd || postStart > postEnd) {\n return nullptr;\n }\n // root is at postEnd\n TreeNode* root = new TreeNode(postorder[postEnd]);\n int idx = inOrderIdx[root->val + 3000];\n int rightLength = inEnd - idx;\n // cout << postStart << ' ' << postEnd << ' ' << rightLength << '\\n';\n // assert(rightLength >= 0);\n // elements to left of root in order are left subtree\n root->left = construct(inStart, idx - 1, postStart,\n postEnd - rightLength - 1, inorder, postorder, inOrderIdx);\n // elements to right in order are right subtree\n root->right = construct(idx + 1, inEnd, postEnd - rightLength,\n postEnd - 1, inorder, postorder, inOrderIdx);\n return root;\n }\n\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n = inorder.size();\n vector<int> inOrderIdx(6001, -1);\n for (int i = 0; i < n; i++) {\n inOrderIdx[inorder[i] + 3000] = i;\n }\n return construct(0, n-1, 0, n-1, inorder, postorder, inOrderIdx);\n }\n};",
"memory": "30644"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTreeHelper(vector<int>& inorderIndexMap,int startIndex, int lastIndex, vector<int>& postorder)\n {\n //cout<<startIndex<<\" \"<<lastIndex<<endl;\n if(startIndex>lastIndex)\n return NULL;\n \n int n = postorder.size();\n int value = postorder[n-1];\n postorder.pop_back();\n \n int index = inorderIndexMap[value + 3000];\n TreeNode* root = new TreeNode;\n (*root).val = value;\n (*root).right = buildTreeHelper(inorderIndexMap, index+1, lastIndex, postorder);\n (*root).left = buildTreeHelper(inorderIndexMap, startIndex, index-1, postorder);\n return root;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n vector<int> inorderIndexMap(6001);\n int n = inorder.size();\n for(int i=0; i<n; i++)\n {\n inorderIndexMap[inorder[i]+3000] = i;\n }\n return buildTreeHelper(inorderIndexMap, 0, n-1, postorder);\n }\n};",
"memory": "31885"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* solve(vector<int>&inorder, vector<int>& postorder, int inorderstart, int inorderend, int& postorderindex,unordered_map<int,stack<int>>& mp){\n if(inorderend<inorderstart || postorderindex<0){\n return NULL;\n }\n int root = postorder[postorderindex--];\n TreeNode* node = new TreeNode(root);\n int pos = mp[root].top();\n mp[root].pop();\n node->right = solve(inorder,postorder,pos+1,inorderend,postorderindex,mp);\n node->left = solve(inorder,postorder,inorderstart,pos-1,postorderindex,mp);\n return node;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int postorderindex =postorder.size()-1;\n unordered_map<int,stack<int>>mp;\n for(int i =0;i<inorder.size();i++){\n mp[inorder[i]].push(i);\n }\n return solve(inorder,postorder,0,inorder.size()-1,postorderindex,mp);\n }\n};",
"memory": "31885"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int post_ind;\n TreeNode* helper(vector<int> &in,vector<int> &post,int start,int end,unordered_map<int,queue<int>> &ump){\n if(start>end) return NULL;\n int data = post[post_ind];\n post_ind--;\n TreeNode* root = new TreeNode(data);\n auto &q = ump[data];\n int index = q.front();\n q.pop();\n root->right = helper(in,post,index+1,end,ump);\n root->left = helper(in,post,start,index-1,ump);\n return root;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n = inorder.size();\n post_ind = n-1;\n unordered_map<int,queue<int>> ump;\n for(int i = 0; i<n; i++){\n ump[inorder[i]].push(i);\n }\n return helper(inorder,postorder,0,n-1,ump);\n }\n};",
"memory": "33126"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n TreeNode* solve(int &ind, int low, int position, vector<int>& inorder, vector<int>& postorder, unordered_map<int, queue<int>>& mp) {\n if (low > position || ind < 0) return nullptr;\n\n int element = postorder[ind--];\n int pos = mp[element].front();\n mp[element].pop();\n\n TreeNode* root = new TreeNode(element);\n\n root->right = solve(ind, pos + 1, position, inorder, postorder, mp);\n root->left = solve(ind, low, pos - 1, inorder, postorder, mp);\n\n return root;\n }\n\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n unordered_map<int, queue<int>> mp;\n int n = inorder.size();\n for (int i = 0; i < n; i++) {\n mp[inorder[i]].push(i);\n }\n int ind = n - 1; \n return solve(ind, 0, n - 1, inorder, postorder, mp);\n }\n};\n",
"memory": "33126"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (inorder.empty() || postorder.empty()) return nullptr;\n TreeNode* root = new TreeNode(postorder.back());\n postorder.pop_back();\n auto it = find(inorder.begin(),inorder.end(),root->val);\n int ind = distance(inorder.begin(),it);\n\n\n\n vector<int> leftInorder(inorder.begin(),inorder.begin()+ind);\n vector<int> rightInorder(inorder.begin()+ind+1,inorder.end());\n\n\n\n root->right = buildTree(rightInorder, postorder);\n root->left = buildTree(leftInorder, postorder);\n \n return root;\n \n }\n};",
"memory": "34368"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* build(vector<int>& inorder, vector<int>& postorder){\n if (inorder.empty()) return nullptr;\n\n int a = postorder.back();\n postorder.pop_back();\n\n int idx = 0;\n while (inorder[idx] != a) {\n idx++;\n }\n \n vector<int> right_inorder(inorder.begin()+idx+1, inorder.end());\n vector<int> left_inorder(inorder.begin(), inorder.begin()+idx);\n\n TreeNode* root = new TreeNode(a);\n\n root->right = build(right_inorder, postorder);\n root->left = build(left_inorder, postorder);\n \n return root;\n\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n return build(inorder, postorder);\n }\n};",
"memory": "35609"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nTreeNode* makeTree(vector<vector<pair<int, bool>>> &adj, int u){\n TreeNode* node = new TreeNode(u);\n for(auto &&X: adj[u + 3000]){\n if(!X.second) node->left = makeTree(adj, X.first);\n else node->right = makeTree(adj, X.first);\n }\n return node;\n}\n\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n = postorder.size();\n reverse(postorder.begin(), postorder.end());\n pair<int, bool> parent[6010];\n pair<int, int> lim[6010];\n int ind[6010];\n for(int i = 0; i < n; i++) ind[inorder[i] + 3000] = i;\n int index = ind[postorder[0] + 3000];\n lim[postorder[0] + 3000] = {0, n};\n for(int j = 0; j < index; j++) parent[inorder[j] + 3000] = {postorder[0], 0};\n for(int j = index + 1; j < n; j++) parent[inorder[j] + 3000] = {postorder[0], 1};\n for(int i = 1; i < n; i++){\n index = ind[postorder[i] + 3000];\n int parIndex = ind[parent[postorder[i] + 3000].first + 3000];\n int l = lim[parent[postorder[i] + 3000].first + 3000].first, u = lim[parent[postorder[i] + 3000].first + 3000].second;\n if(index > parIndex) l = parIndex + 1;\n else u = parIndex;\n lim[postorder[i] + 3000] = {l, u};\n for(int j = l; j < index; j++) parent[inorder[j] + 3000] = {postorder[i], 0};\n for(int j = index + 1; j < u; j++) parent[inorder[j] + 3000] = {postorder[i], 1};\n }\n vector<vector<pair<int, bool>>> adj(6010);\n for(int i = 1; i < n; i++) adj[parent[postorder[i] + 3000].first + 3000].push_back({postorder[i], parent[postorder[i] + 3000].second});\n TreeNode* root = makeTree(adj, postorder[0]);\n return root;\n }\n};",
"memory": "41815"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int search(vector<int> inorder,int start,int end,int curr){\n for(int i=start;i<=end;i++){\n if(inorder[i]==curr){\n return i;\n }\n }\n return -1;\n }\n TreeNode* helper(vector<int>&inorder,vector<int>&postorder,int start,int end,int& idx){\n if(start>end){\n return nullptr;\n }\n int curr=postorder[idx];\n idx--;\n TreeNode* Node=new TreeNode(curr);\n if(start==end){\n return Node;\n }\n int pos=search(inorder,start,end,curr);\n Node->right=helper(inorder,postorder,pos+1,end,idx);\n Node->left=helper(inorder,postorder,start,pos-1,idx);\n return Node;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int idx=inorder.size()-1;\n TreeNode* root=helper(inorder,postorder,0,inorder.size()-1,idx);\n return root;\n }\n};",
"memory": "43056"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int idx;\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int start=0;\n int end=inorder.size()-1;\n idx=postorder.size()-1;\n TreeNode* root = build(inorder,postorder,start,end);\n return root;\n }\nprivate:\n \n int search(vector<int> inorder ,int val,int start,int end){\n for(int i=start;i<=end;i++){\n if(inorder[i]==val){\n return i;\n }\n }\n return -1;\n }\n TreeNode* build(vector<int>& inorder, vector<int>& postorder,int start,int end){\n if(end<start){\n return NULL;\n }\n int val=postorder[idx];\n idx--;\n TreeNode* node=new TreeNode(val);\n if(start==end){\n return node;\n }\n int curr = search(inorder,val,start,end);\n node->right=build(inorder,postorder,curr+1,end);\n node->left=build(inorder,postorder,start,curr-1);\n return node;\n\n\n }\n};",
"memory": "44298"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int search(vector<int> inorder,int start,int end,int curr){\n for(int i=start;i<=end;i++){\n if(inorder[i]==curr){\n return i;\n }\n }\n return -1;\n }\n TreeNode* helper(vector<int>&inorder,vector<int>&postorder,int start,int end,int& idx){\n if(start>end){\n return nullptr;\n }\n int curr=postorder[idx];\n idx--;\n TreeNode* Node=new TreeNode(curr);\n if(start==end){\n return Node;\n }\n int pos=search(inorder,start,end,curr);\n Node->right=helper(inorder,postorder,pos+1,end,idx);\n Node->left=helper(inorder,postorder,start,pos-1,idx);\n return Node;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int idx=inorder.size()-1;\n TreeNode* root=helper(inorder,postorder,0,inorder.size()-1,idx);\n return root;\n }\n};",
"memory": "44298"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int gstart;\n \n int search(vector<int>inorder, int x,int start,int end)\n {\n for(int i=start;i<=end;i++)\n {\n if(inorder[i]==x)\n {\n return i;\n }\n }\n \n return -1;\n }\n \n \n TreeNode* maketree(vector<int>& postorder, vector<int>& inorder, int start, int end)\n {\n if(gstart<0 or start>end)\n {\n return NULL;\n }\n \n TreeNode* root=new TreeNode(postorder[gstart--]);\n \n if(start==end)\n {\n return root;\n }\n \n \n // gstart++;\n \n \n int index = search(inorder,root->val,start,end);\n \n root->right=maketree(postorder,inorder,index+1,end);\n root->left=maketree(postorder,inorder,start,index-1);\n \n \n return root;\n \n \n }\n \n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n gstart=postorder.size()-1; \n return maketree(postorder,inorder,0,postorder.size()-1); \n }\n};",
"memory": "45539"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "// /**\n// * Definition for a binary tree node.\n// * struct TreeNode {\n// * int val;\n// * TreeNode *left;\n// * TreeNode *right;\n// * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n// * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n// * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n// * };\n// */\n// class Solution {\n// public:\n// int search(vector<int> &inorder,int start,int end,int curr){\n// for(int i=start;i<=end;i++){\n// if(inorder[i]==curr){\n// return i;\n// }\n// }\n// return -1;\n// }\n\n// TreeNode*solveByRec(vector<int>& inorder, vector<int>& postorder,int pi,int pe,int &index){\n// if(pi > pe) {\n// return nullptr;\n// }\n\n// int curr = postorder[index];\n// index--;\n// TreeNode*newnode = new TreeNode(curr);\n// if(pi == pe){\n// return newnode;\n// }\n// int pos = search(inorder,pi,pe,curr);\n\n// TreeNode*right = solveByRec(inorder,postorder,pos+1, pe,index);\n// TreeNode*left = solveByRec(inorder,postorder,pi, pos-1,index);\n// return newnode;\n// }\n// TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n// int index = inorder.size()-1;\n// TreeNode* root = solveByRec(inorder,postorder,0,inorder.size()-1,index);\n// return root;\n\n// }\n// };\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int search(vector<int> inorder,int start,int end,int curr){\n for(int i=start;i<=end;i++){\n if(inorder[i]==curr){\n return i;\n }\n }\n return -1;\n }\n TreeNode* helper(vector<int>&inorder,vector<int>&postorder,int start,int end,int& idx){\n if(start>end){\n return nullptr;\n }\n int curr=postorder[idx];\n idx--;\n TreeNode* Node=new TreeNode(curr);\n if(start==end){\n return Node;\n }\n int pos=search(inorder,start,end,curr);\n Node->right=helper(inorder,postorder,pos+1,end,idx);\n Node->left=helper(inorder,postorder,start,pos-1,idx);\n return Node;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int idx=inorder.size()-1;\n TreeNode* root=helper(inorder,postorder,0,inorder.size()-1,idx);\n return root;\n }\n};",
"memory": "46780"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int search(vector<int> inorder,int start,int end,int curr){\n for(int i=start;i<=end;i++){\n if(inorder[i]==curr){\n return i;\n }\n }\n return -1;\n }\n TreeNode* helper(vector<int>&inorder,vector<int>&postorder,int start,int end,int& idx){\n if(start>end){\n return nullptr;\n }\n int curr=postorder[idx];\n idx--;\n TreeNode* Node=new TreeNode(curr);\n if(start==end){\n return Node;\n }\n int pos=search(inorder,start,end,curr);\n Node->right=helper(inorder,postorder,pos+1,end,idx);\n Node->left=helper(inorder,postorder,start,pos-1,idx);\n return Node;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int idx=inorder.size()-1;\n TreeNode* root=helper(inorder,postorder,0,inorder.size()-1,idx);\n return root;\n }\n};",
"memory": "46780"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n TreeNode* root_node = new TreeNode(postorder[postorder.size()-1]);\n vector<int>::iterator it = inorder.begin();\n for(; it != inorder.end(); it++)\n {\n if((*it) == postorder[postorder.size()-1])\n break;\n }\n\n vector<int>* inorder_left = initialize_left_inorder(it, &inorder);\n vector<int>* inorder_right = initialize_right_inorder(it, &inorder);\n vector<int>* postorder_right = initialize_right_postorder(inorder_right->size(), &postorder);\n vector<int>* postorder_left = initialize_left_postorder(inorder_right->size(), inorder_left->size(), &postorder);\n \n build_tree(root_node, inorder_left, inorder_right, postorder_left, postorder_right);\n\n return root_node;\n }\n\n void build_tree(TreeNode* root, vector<int>* in_left, vector<int>* in_right, vector<int>* post_left, vector<int>* post_right)\n {\n if((in_left->size() == 0 || post_left->size() == 0) && (in_right->size() == 0 || post_right->size() == 0))\n return;\n\n if(post_left->size() > 0 && in_left->size() > 0)\n {\n vector<int>::iterator it = in_left->begin();\n for(; it != in_left->end(); it++)\n {\n if((*it) == post_left->at(post_left->size()-1))\n break;\n }\n vector<int>* inorder_left = initialize_left_inorder(it, in_left);\n vector<int>* inorder_right = initialize_right_inorder(it, in_left);\n vector<int>* postorder_right = initialize_right_postorder(inorder_right->size(), post_left);\n vector<int>* postorder_left = initialize_left_postorder(inorder_right->size(), inorder_left->size(), post_left);\n\n TreeNode* node = new TreeNode(post_left->at(post_left->size()-1));\n root->left = node;\n\n build_tree(node, inorder_left, inorder_right, postorder_left, postorder_right);\n }\n\n if(post_right->size() > 0 && in_right->size() > 0)\n {\n vector<int>::iterator it = in_right->begin();\n for(; it != in_right->end(); it++)\n {\n if((*it) == post_right->at(post_right->size()-1))\n break;\n }\n vector<int>* inorder_left = initialize_left_inorder(it, in_right);\n vector<int>* inorder_right = initialize_right_inorder(it, in_right);\n vector<int>* postorder_right = initialize_right_postorder(inorder_right->size(), post_right);\n vector<int>* postorder_left = initialize_left_postorder(inorder_right->size(), inorder_left->size(), post_right);\n\n TreeNode* node = new TreeNode(post_right->at(post_right->size()-1));\n root->right = node;\n\n build_tree(node, inorder_left, inorder_right, postorder_left, postorder_right);\n }\n }\n\n vector<int>* initialize_left_inorder(vector<int>::iterator it, vector<int>* left_vector)\n {\n if(it == left_vector->begin())\n {\n //std::cout << \"initialize_left_inorder first\" << std::endl;\n return new vector<int>();\n }\n else\n {\n //std::cout << \"initialize_left_inorder second\" << std::endl;\n vector<int>::iterator itr = left_vector->begin();\n /*for(; itr < it; itr++)\n std::cout << (*itr) << \", \";\n std::cout << std::endl;*/\n return new vector<int>(left_vector->begin(), it);\n }\n }\n\n vector<int>* initialize_right_inorder(vector<int>::iterator it, vector<int>* right_vector)\n {\n if((it+1) != right_vector->end())\n {\n //std::cout << \"initialize_right_inorder first\" << std::endl;\n vector<int>::iterator itr = it+1;\n /*for(; itr < right_vector->end(); itr++)\n std::cout << (*itr) << \", \";\n std::cout << std::endl;*/\n return new vector<int>(it+1, right_vector->end());\n }\n else\n {\n //std::cout << \"initialize_right_inorder second\" << std::endl;\n return new vector<int>();\n }\n }\n\n vector<int>* initialize_left_postorder(int right_size, int left_size, vector<int>* postorder)\n {\n if(left_size > 0)\n {\n //std::cout << \"initialize_left_preorder \" << left_size << std::endl;\n vector<int>::iterator it = postorder->begin();\n /*(for(; it < postorder->begin()+left_size+1; it++)\n std::cout << (*it) << \", \";\n std::cout << std::endl;*/\n return new vector<int>(postorder->begin(), postorder->begin()+left_size);\n }\n else\n {\n //std::cout << \"initialize_left_preorder \" << left_size << std::endl;\n return new vector<int>();\n }\n }\n\n vector<int>* initialize_right_postorder(int right_size, vector<int>* postorder)\n {\n if(right_size > 0)\n {\n //std::cout << \"initialize_right_preorder \" << right_size << std::endl;\n vector<int>::iterator it = postorder->end()-right_size-1;\n /*for(; it < postorder->end()-1; it++)\n std::cout << (*it) << \", \";\n std::cout << std::endl;*/\n return new vector<int>(postorder->end()-right_size-1, postorder->end()-1);\n }\n else\n {\n //std::cout << \"initialize_right_preorder \" << right_size << std::endl;\n return new vector<int>();\n }\n } \n};",
"memory": "48021"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> slice(const vector<int>& input, int start, int end) {\n return vector<int>(input.begin() + start, input.begin() + end);\n}\n\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n \n if(inorder.size()==0){\n return NULL;\n }\n int size = postorder.size();\n int root_val = postorder[size-1];\n\n TreeNode* root = new TreeNode(root_val);\n\n int root_index=-1;\n\n for(int i=0;i<size;i++){\n if(inorder[i]==root_val){\n root_index=i;\n }\n }\n vector<int>left_in=slice(inorder,0,root_index);\n vector<int>right_in=slice(inorder,root_index+1,size);\n \n vector<int>left_pos=slice(postorder,0,root_index);\n vector<int>right_pos=slice(postorder,root_index,size-1);\n\n TreeNode*left_tree =buildTree(left_in,left_pos);\n TreeNode*right_tree=buildTree(right_in,right_pos);\n\n root->left=left_tree;\n root->right=right_tree;\n return root;\n }\n};",
"memory": "49263"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n=inorder.size();\n if(n==0)\n return NULL;\n TreeNode *root=new TreeNode();\n root->val=postorder.back();\n auto itr=find(inorder.begin(),inorder.end(),root->val);\n if(itr!=inorder.end()){\n int rootind=distance(inorder.begin(),itr);\n int nleft=rootind,nright=n-rootind-1;\n cout<<nleft<<\"-\"<<nright<<endl;\n if(nleft>0){\n vector<int> leftinorder(inorder.begin(),inorder.begin()+nleft);\n vector<int> leftpostorder(postorder.begin(),postorder.begin()+nleft);\n cout<<leftinorder.size()<<\"-leftorder-\"<<leftpostorder.size()<<endl;\n root->left=buildTree(leftinorder,leftpostorder);\n }\n else root->left=NULL;\n if(nright>0){\n vector<int> rightinorder(inorder.begin()+nleft+1,inorder.end());\n vector<int> rightpostorder(postorder.begin()+nleft,postorder.end()-1);\n cout<<rightinorder.size()<<\"-rightorder-\"<<rightpostorder.size()<<endl;\n root->right=buildTree(rightinorder,rightpostorder);\n }\n else root->right=NULL;\n }\n return root;\n \n }\n};",
"memory": "50504"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (postorder.size() == 0) return nullptr;\n if (postorder.size() == 1) return new TreeNode(*postorder.rbegin());\n TreeNode* root = new TreeNode(*(postorder.rbegin()));\n auto rootInOrder = find(inorder.begin(), inorder.end(), *(postorder.rbegin()));\n vector<int> leftInOrder(inorder.begin(), rootInOrder);\n vector<int> rightInOrder(rootInOrder + 1, inorder.end());\n vector<int> leftPostOrder(postorder.begin(), postorder.begin() + leftInOrder.size());\n vector<int> rightPostOrder(postorder.begin() + leftInOrder.size(), postorder.end()-1);\n\n root->left = buildTree(leftInOrder, leftPostOrder);\n root->right = buildTree(rightInOrder, rightPostOrder);\n\n return root;\n\n }\n};",
"memory": "51745"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (inorder.empty() || postorder.empty()) return nullptr;\n TreeNode* root = new TreeNode();\n root->val = *(postorder.end()-1);\n auto inpoint = inorder.begin(), postpoint = postorder.begin();\n while (*inpoint != root->val)\n {\n inpoint += 1;\n postpoint += 1;\n }\n vector<int> inorderleft(inorder.begin(), inpoint);\n vector<int> inorderright(inpoint+1, inorder.end());\n vector<int> postorderleft(postorder.begin(), postpoint);\n vector<int> postorderright (postpoint, postorder.end()-1);\n root->left = buildTree(inorderleft, postorderleft);\n root->right = buildTree(inorderright, postorderright);\n return root;\n }\n};",
"memory": "52986"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n //arr1->in ; arr2->post\n TreeNode* buildTree(vector<int>& arr1, vector<int>& arr2) {\n if (!arr1.size() || !arr2.size()) return nullptr;\n \n TreeNode* root = new TreeNode(arr2[arr2.size()-1]);\n \n int index = 0;\n for(auto i: arr1){\n if(i==arr2[arr2.size()-1]) break;\n index++;\n }\n \n vector<int> inorder1(arr1.begin(), arr1.begin() + index);\n vector<int> postorder1(arr2.begin(), arr2.begin() + index);\n\n root->left = buildTree(inorder1, postorder1);\n \n vector<int> inorder2(arr1.begin() + index + 1, arr1.end());\n vector<int> postorder2(arr2.begin() + index, arr2.end() - 1);\n\n root->right = buildTree(inorder2, postorder2);\n \n return root;\n }\n};\n",
"memory": "52986"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if(!postorder.size() || !inorder.size()) return nullptr;\n\n int n = postorder.size();\n\n TreeNode* root = new TreeNode(postorder[n-1]);\n\n int mid = find(inorder.begin(),inorder.end(),postorder[n-1]) - inorder.begin();\n\n vector<int>leftin(inorder.begin(),inorder.begin()+mid);\n vector<int>rightin(inorder.begin()+mid+1,inorder.end());\n\n \n vector<int>leftpost(postorder.begin(),postorder.begin()+mid);\n vector<int>rightpost(postorder.begin()+mid,postorder.end()-1);\n\n root->left = buildTree(leftin,leftpost);\n root->right = buildTree(rightin,rightpost);\n\n return root;\n\n }\n};",
"memory": "54228"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (inorder.size() == 0 || postorder.size() == 0){\n return NULL;\n }\n return construct(inorder, postorder);\n }\n\n TreeNode* construct(vector<int>& inorder, vector<int>& postorder){\n\n if (inorder.size() == 0 || postorder.size() == 0) return NULL;\n\n int curr_node = postorder[postorder.size() - 1];\n TreeNode* root = new TreeNode(curr_node);\n int delimiterIndex;\n for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {\n if (inorder[delimiterIndex] == curr_node) break;\n }\n\n vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);\n vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end());\n\n postorder.resize(postorder.size() - 1);\n\n vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());\n vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());\n\n root->left = construct(leftInorder, leftPostorder);\n root->right = construct(rightInorder, rightPostorder);\n\n return root;\n\n }\n};",
"memory": "54228"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (postorder.size() == 0) return nullptr;\n if (postorder.size() == 1) return new TreeNode(*postorder.rbegin());\n TreeNode* root = new TreeNode(*(postorder.rbegin()));\n auto rootInOrder = find(inorder.begin(), inorder.end(), *(postorder.rbegin()));\n vector<int> leftInOrder(inorder.begin(), rootInOrder);\n vector<int> rightInOrder(rootInOrder + 1, inorder.end());\n vector<int> leftPostOrder(postorder.begin(), postorder.begin() + leftInOrder.size());\n vector<int> rightPostOrder(postorder.begin() + leftInOrder.size(), postorder.end()-1);\n\n root->left = buildTree(leftInOrder, leftPostOrder);\n root->right = buildTree(rightInOrder, rightPostOrder);\n\n return root;\n\n }\n};",
"memory": "55469"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n Solution(){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n TreeNode* buildTree(vector<int> inorder, vector<int> postorder) {\n int root_val = -3001;\n int root_idx = -1;\n int n = inorder.size();\n if (n == 0) return 0;\n\n for (int i=0; i<n; i++){\n if (postorder[n-1] == inorder[i]){\n root_val = inorder[i];\n root_idx = i;\n }\n }\n TreeNode* root = new TreeNode(root_val);\n\n // vector<int> tmp (postorder.begin()+root_idx, postorder.end()-1);\n // cout << tmp.size() << endl;\n // for (int i: tmp){\n // cout << i << endl;\n // }\n // cout <<\"=========\" << endl;\n\n root->left = (n==1)? 0: buildTree(vector<int>(inorder.begin(), inorder.begin()+root_idx), vector<int>(postorder.begin(), postorder.begin()+root_idx));\n root->right = (n==1)? 0: buildTree(vector<int>(inorder.begin()+1+root_idx, inorder.end()), vector<int>(postorder.begin()+root_idx, postorder.end()-1));\n\n return root;\n \n }\n};",
"memory": "56710"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if(postorder.size() == 1 && inorder.size() == 1){\n TreeNode *node = new TreeNode(inorder[0]);\n return node;\n }\n else{\n int nval = postorder[postorder.size()-1];\n\n int place = find(inorder.begin(), inorder.end(), nval) - inorder.begin();\n\n vector<int> intemp1(inorder.begin(), inorder.begin() + place);\n vector<int> intemp2(inorder.begin() + place + 1, inorder.end());\n\n vector<int> posttemp1(postorder.rbegin() + intemp2.size() + 1, postorder.rend());\n vector<int> posttemp2(postorder.rbegin() + 1, postorder.rbegin() + intemp2.size() + 1);\n\n reverse(posttemp1.begin(), posttemp1.end());\n reverse(posttemp2.begin(), posttemp2.end());\n\n TreeNode* node;\n\n if(intemp1.size() == 0)\n node = new TreeNode(nval, nullptr, buildTree(intemp2, posttemp2));\n else if(intemp2.size() == 0)\n node = new TreeNode(nval, buildTree(intemp1, posttemp1), nullptr);\n else if(intemp1.size() == 0 && intemp2.size() == 0)\n node = new TreeNode(nval);\n else\n node = new TreeNode(nval, buildTree(intemp1, posttemp1), buildTree(intemp2, posttemp2));\n return node;\n }\n }\n};",
"memory": "57951"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if(inorder.size()==0) return NULL;\n if(inorder.size()==1) return new TreeNode(inorder[0]);\n \n TreeNode* root = new TreeNode(postorder.back());\n\n vector<int> post;\n vector<int> inor;\n int index = 0;\n for(int i=inorder.size()-1;i>=0;--i){\n if(inorder[i]==root->val){\n index = i;\n break;\n }\n } \n\n\n inor.insert(inor.end(),inorder.begin(),inorder.begin()+index);\n post.insert(post.end(),postorder.begin(),postorder.begin()+inor.size());\n root->left = buildTree(inor,post);\n inor.clear();\n post.clear();\n\n inor.insert(inor.end(),inorder.begin()+index+1,inorder.end());\n post.insert(post.end(),postorder.end()-1-inor.size(),postorder.end()-1); \n root->right = buildTree(inor,post);\n inor.clear();\n post.clear();\n\n return root;\n }\n};",
"memory": "59193"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n TreeNode* root = new TreeNode;\n\n if(inorder.size()==0 || postorder.size()==0)\n return nullptr;\n \n root->val = postorder[postorder.size()-1];\n\n auto it = find(inorder.begin(), inorder.end(), root->val);\n int mid = distance(inorder.begin(), it);\n\n vector<int> p_l(postorder.begin(), postorder.begin()+mid);\n vector<int> p_r(postorder.begin()+mid, postorder.end()-1);\n\n vector<int> i_l(inorder.begin(), inorder.begin()+mid);\n vector<int> i_r(inorder.begin()+mid+1, inorder.end());\n\n root->left = buildTree(i_l, p_l);\n root->right = buildTree(i_r, p_r);\n\n return root;\n }\n};",
"memory": "60434"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<int, int> hash;\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (inorder.size() == 0) return nullptr;\n for (int i = 0; i < inorder.size(); ++i) hash[inorder[i]] = i;\n int root = postorder.back();\n int index = hash[root];\n vector<int> in_left(inorder.begin(), inorder.begin() + index);\n vector<int> in_right(inorder.begin() + index + 1, inorder.end());\n vector<int> post_left(postorder.begin(), postorder.begin() + in_left.size());\n vector<int> post_right(postorder.begin() + in_left.size(), postorder.end() - 1);\n TreeNode* left_tree = buildTree(in_left, post_left);\n TreeNode* right_tree = buildTree(in_right, post_right);\n TreeNode* root_node = new TreeNode(root);\n root_node->left = left_tree;\n root_node->right = right_tree;\n return root_node;\n }\n};",
"memory": "61675"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (postorder.size() == 1) {\n /* Only 1 element */\n TreeNode *a = new TreeNode(postorder[0]);\n return a;\n }\n /* The base element will always be the last one traversed in postorder */\n int base = postorder[postorder.size() - 1];\n int i = 0;\n while (inorder[i] != base) {\n i++;\n }\n /* The elements on the left side are those before the base, \n and the elements on the right side are those after the base. */\n vector<int> newInOrder(inorder);\n vector<int> newPostOrder(postorder);\n if (i == 0) {\n /* There are only elements on the right side */\n newInOrder.erase(newInOrder.begin());\n newPostOrder.erase(newPostOrder.end());\n TreeNode *right = new TreeNode(base, nullptr, buildTree(newInOrder, newPostOrder));\n return right;\n }\n else if (i == inorder.size() - 1) {\n /* There are only elements on the left side */\n newInOrder.erase(newInOrder.end());\n newPostOrder.erase(newPostOrder.end());\n TreeNode *left = new TreeNode(base, buildTree(newInOrder, newPostOrder), nullptr);\n return left;\n }\n else {\n /* We'll need to split it up into left and right sides */\n vector<int> leftInOrder(inorder);\n vector<int> rightInOrder(inorder);\n vector<int> leftPostOrder(postorder);\n vector<int> rightPostOrder(postorder);\n leftInOrder.erase(leftInOrder.begin() + i, leftInOrder.end());\n rightInOrder.erase(rightInOrder.begin(), rightInOrder.begin() + i + 1);\n // int firstOnRight = inorder[i+1];\n // int j = 0;\n // while (postorder[j] != firstOnRight) {\n // j++;\n // }\n leftPostOrder.erase(leftPostOrder.begin() + i, leftPostOrder.end());\n rightPostOrder.erase(rightPostOrder.begin(), rightPostOrder.begin() + i);\n rightPostOrder.erase(rightPostOrder.end());\n TreeNode *out = new TreeNode(base, \n buildTree(leftInOrder, leftPostOrder), \n buildTree(rightInOrder, rightPostOrder)\n );\n return out;\n }\n }\n};",
"memory": "62916"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int findpos(vector<int>in,int element){\n for(int i=0;i<in.size();i++){\n if(in[i]==element){\n return i;\n }\n }\n return -1;\n }\n TreeNode*solve(vector<int>& inorder, vector<int>& postorder,int &index,int inorderstart,int inorderend){\n if(index<0||inorderstart>inorderend)return NULL;\n int ele=postorder[index--];\n int pos=findpos(inorder,ele);\n TreeNode*root=new TreeNode(ele);\n root->right=solve(inorder,postorder,index,pos+1,inorderend);\n root->left=solve(inorder,postorder,index,inorderstart,pos-1);\n return root;\n }\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int index=postorder.size()-1;\n int inorderstart=0;\n int inorderend=inorder.size()-1;\n return solve(inorder,postorder,index,inorderstart,inorderend);\n }\n};",
"memory": "64158"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n int findPosition(vector<int> inorder,int element){\n for(int i=0;i<inorder.size();i++){\n if(inorder[i]==element)return i;\n }\n return -1;\n }\n\n TreeNode* solve(vector<int> &postorder,vector<int> &inorder,int &postOrderIndex,int startOfInorder,int endOfInorder){\n \n if(postOrderIndex < 0 || startOfInorder > endOfInorder) return nullptr;\n\n int element=postorder[postOrderIndex--];\n TreeNode* root = new TreeNode(element);\n int position=findPosition(inorder,element);\n\n //make tree outof the position from the inorder vector\n root->right=solve(postorder,inorder,postOrderIndex,position+1,endOfInorder);\n root->left=solve(postorder,inorder,postOrderIndex,startOfInorder,position-1);\n \n return root;\n\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int postOrderIndex=postorder.size()-1;\n //TreeNode* ans=solve(postorder,inorder,postOrderIndex,0,inorder.size()-1);\n return solve(postorder,inorder,postOrderIndex,0,inorder.size()-1);\n }\n};",
"memory": "65399"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int findPos(vector<int> in, int element){\n for(int i=0; i<in.size(); i++){\n if(in[i]==element){\n return i;\n }\n }\n return -1;\n }\n TreeNode* solve(vector<int>& in,vector<int>& post,int &index,int inorderStart,int inorderEnd){\n if(index<0 || inorderStart > inorderEnd){\n return NULL;\n }\n int element = post[index];\n index--;\n int position = findPos(in,element);\n TreeNode* root = new TreeNode(element);\n root->right = solve(in,post,index,position + 1,inorderEnd);\n root->left = solve(in,post,index,inorderStart,position-1);\n return root;\n }\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int index = postorder.size()-1;\n int inorderStart = 0;\n int inorderEnd = inorder.size()-1;\n return solve(inorder,postorder,index,inorderStart,inorderEnd);\n }\n};",
"memory": "66640"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nint find_pos(vector<int>inorder,int ele){\n for(int i=0;i<inorder.size();i++){\n if(inorder[i]==ele){\n return i;\n }\n }\n return -1;\n }\n \n \n TreeNode* solve(vector<int>& inorder, vector<int>&postorder,int &idx,int instart,int inend,int n){\n if(idx<0 || instart>inend ){\n return NULL;\n }\n \n int element=postorder[idx--];\n TreeNode* root=new TreeNode(element);\n int pos=find_pos(inorder,element);\n root->right=solve(inorder,postorder,idx,pos+1,inend, n);\n root->left=solve(inorder,postorder,idx,instart,pos-1, n);\n \n return root;\n\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n=inorder.size();\n int postIndex=n-1;\n TreeNode* ans=solve(inorder,postorder,postIndex,0,n-1, n);\n return ans;\n }\n};",
"memory": "67881"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int findPosition(vector<int> arr, int num){\n for(int i=0;i<arr.size();i++){\n if (arr[i]==num){\n return i;\n }\n }\n return -1;\n }\n TreeNode* treeHelper(vector<int>& inorder, vector<int>& postorder,int &index,int start,int end){\n if(index<0 || start>end){\n return NULL;\n }\n int element=postorder[index--];\n TreeNode* root=new TreeNode(element);\n int position=findPosition(inorder,element);\n root->right=treeHelper(inorder,postorder,index,position + 1,end);\n root->left= treeHelper(inorder,postorder,index,start,position - 1);\n return root;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n=inorder.size()-1;\n return treeHelper(inorder,postorder,n,0,n);\n }\n};",
"memory": "69123"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic: \n int findpos(vector<int> inorder,int element,int n){\n for(int i=0; i<n; i++){\n if(inorder[i] == element){\n return i;\n }\n }\n return -1;\n }\n\n TreeNode* solve(vector<int>& inorder, vector<int>& postorder,int &index , int inindexs,int inindexe,int n){\n if(index < 0 || inindexs > inindexe){\n return NULL;\n }\n int element = postorder[index--];\n TreeNode* root = new TreeNode(element);\n int pos = findpos(inorder,element,n);\n \n root->right = solve(inorder,postorder,index,pos+1,inindexe,n);\n root->left = solve(inorder,postorder,index,inindexs,pos-1,n);\n return root;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n = inorder.size();\n int postindex = n-1;\n TreeNode* ans = solve(inorder,postorder,postindex,0,n-1,n);\n return ans;\n }\n};",
"memory": "70364"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {\n\n function<TreeNode *(vector<int> &, vector<int> &)> build =\n [&build](vector<int> &inorder, vector<int> &postorder) -> TreeNode * {\n if (inorder.empty() || postorder.empty())\n return nullptr;\n\n auto val = *postorder.rbegin();\n auto it = find(inorder.begin(), inorder.end(), val);\n size_t idx = it - inorder.begin();\n\n vector<int> left_inorder, left_postorder, right_inorder, right_postorder;\n\n left_inorder = vector(inorder.begin(), it);\n right_inorder = vector(it + 1, inorder.end());\n left_postorder =\n vector(postorder.begin(), postorder.begin() + left_inorder.size());\n right_postorder =\n vector(postorder.begin() + left_inorder.size(), postorder.end() - 1);\n\n auto root = new TreeNode(val);\n root->left = build(left_inorder, left_postorder);\n root->right = build(right_inorder, right_postorder);\n\n return root;\n };\n\n return build(inorder, postorder);\n }\n};",
"memory": "71605"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n unordered_map<int, int> indices;\n\n for (int i = 0; i < inorder.size(); ++i) {\n indices[inorder[i]] = i;\n }\n\n TreeNode *root = new TreeNode(postorder[postorder.size() - 1]);\n TreeNode *node = root;\n\n stack<TreeNode *> parents;\n parents.push(node);\n\n for (int i = postorder.size() - 2; i >= 0; --i) {\n if (indices[postorder[i]] > indices[node->val]) {\n TreeNode *right = new TreeNode(postorder[i]);\n node->right = right;\n parents.push(node);\n node = node->right;\n } else {\n while (true) {\n node = parents.top();\n\n if (indices[node->val] < indices[postorder[i]]) {\n node = node->right;\n break;\n }\n\n parents.pop();\n\n if (node == root)\n break;\n }\n\n while (node->left) {\n parents.push(node);\n node = node->left;\n }\n\n node->left = new TreeNode(postorder[i]);\n parents.push(node);\n node = node->left;\n }\n }\n\n return root;\n }\n};",
"memory": "72846"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* build(vector<int>post,vector<int>in)\n {\n if(in.size()==1)\n {\n TreeNode* t=new TreeNode(in[0]);\n return t;\n }\n int x=post[post.size()-1];\n TreeNode* temp=new TreeNode(x);\n int it=find(in.begin(),in.end(),x)-in.begin();\n if(it==in.size()-1){\n temp->right=NULL;\n temp->left=build(vector<int>(post.begin(),post.end()-1),vector<int>(in.begin(),in.end()-1));\n }\n else if(it==0)\n {\n temp->left=NULL;\n temp->right=build(vector<int>(post.begin(),post.end()-1),vector<int>(in.begin()+1,in.end()));\n }\n else{\n temp->left=build(vector<int>(post.begin(),post.begin()+it),vector<int>(in.begin(),in.begin()+it));\n temp->right=build(vector<int>(post.begin()+it,post.end()-1),vector<int>(in.begin()+it+1,in.end()));\n }\n return temp;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n return build(postorder,inorder);\n }\n};",
"memory": "74088"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n unordered_map<int, int> indices;\n\n for (int i = 0; i < inorder.size(); ++i) {\n indices[inorder[i]] = i;\n }\n\n TreeNode *root = new TreeNode(postorder[postorder.size() - 1]);\n TreeNode *node = root;\n\n stack<TreeNode *> parents;\n parents.push(node);\n\n for (int i = postorder.size() - 2; i >= 0; --i) {\n if (indices[postorder[i]] > indices[node->val]) {\n TreeNode *right = new TreeNode(postorder[i]);\n node->right = right;\n parents.push(node);\n node = node->right;\n } else {\n while (true) {\n node = parents.top();\n\n if (indices[node->val] < indices[postorder[i]]) {\n node = node->right;\n break;\n }\n\n if (node == root)\n break;\n\n parents.pop();\n }\n\n while (node->left) {\n parents.push(node);\n node = node->left;\n }\n\n node->left = new TreeNode(postorder[i]);\n parents.push(node);\n node = node->left;\n }\n }\n\n return root;\n }\n};",
"memory": "75329"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n unordered_map<int, int> indices;\n\n for (int i = 0; i < inorder.size(); ++i) {\n indices[inorder[i]] = i;\n }\n\n TreeNode *root = new TreeNode(postorder[postorder.size() - 1]);\n TreeNode *node = root;\n\n stack<TreeNode *> parents;\n parents.push(node);\n\n for (int i = postorder.size() - 2; i >= 0; --i) {\n if (indices[postorder[i]] > indices[node->val]) {\n TreeNode *right = new TreeNode(postorder[i]);\n node->right = right;\n parents.push(node);\n node = node->right;\n } else {\n while (true) {\n node = parents.top();\n\n if (indices[node->val] < indices[postorder[i]]) {\n node = node->right;\n break;\n }\n\n if (node == root)\n break;\n\n parents.pop();\n }\n\n while (node->left) {\n parents.push(node);\n node = node->left;\n }\n\n node->left = new TreeNode(postorder[i]);\n parents.push(node);\n node = node->left;\n }\n }\n\n return root;\n }\n};",
"memory": "75329"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n map<int, int> indices;\n\n for (int i = 0; i < inorder.size(); ++i) {\n indices[inorder[i]] = i;\n }\n\n TreeNode *root = new TreeNode(postorder[postorder.size() - 1]);\n TreeNode *node = root;\n\n stack<TreeNode *> parents;\n parents.push(node);\n\n for (int i = postorder.size() - 2; i >= 0; --i) {\n if (indices[postorder[i]] > indices[node->val]) {\n TreeNode *right = new TreeNode(postorder[i]);\n node->right = right;\n parents.push(node);\n node = node->right;\n } else {\n while (true) {\n node = parents.top();\n\n if (indices[node->val] < indices[postorder[i]]) {\n node = node->right;\n break;\n }\n\n if (node == root)\n break;\n\n parents.pop();\n }\n\n while (node->left) {\n parents.push(node);\n node = node->left;\n }\n\n node->left = new TreeNode(postorder[i]);\n parents.push(node);\n node = node->left;\n }\n }\n\n return root;\n }\n};",
"memory": "76570"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n TreeNode* solve(deque<int> &post, deque<int> &in) {\n if (post.empty()) {\n return nullptr;\n }\n int root = post.back();\n post.pop_back();\n\n deque<int> left_in, left_post;\n while (in.size() and in.front() != root) {\n left_in.push_back(in.front());\n in.pop_front();\n }\n in.pop_front();\n while (left_post.size() < left_in.size()) {\n left_post.push_back(post.front());\n post.pop_front();\n }\n TreeNode *left = solve(left_post, left_in);\n TreeNode *right = solve(post, in);\n TreeNode *ret = new TreeNode(root, left, right);\n return ret;\n }\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n deque<int> post{postorder.begin(), postorder.end()};\n deque<int> in{inorder.begin(), inorder.end()};\n return solve(post, in);\n }\n};",
"memory": "77811"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* func(vector<int>& postorder, vector<int>& inorder, int& i, TreeNode* root) {\n if (inorder.empty()) { return nullptr; }\n\n root = new TreeNode(postorder[i--]);\n\n vector<int> temp1;\n vector<int> temp2;\n int breaker;\n\n // Find the index of the root in inorder\n for (int j = 0; j < inorder.size(); j++) {\n if (inorder[j] == root->val) {\n breaker = j;\n break;\n }\n else{temp1.push_back(inorder[j]);}\n }\n\n // Populate temp1 with elements left of the root in inorder\n //for (int j = 0; j < breaker; j++) {\n // temp1.push_back(inorder[j]);\n //}\n\n // Populate temp2 with elements right of the root in inorder\n for (int j = breaker + 1; j < inorder.size(); j++) {\n temp2.push_back(inorder[j]);\n }\n\n // Recursive calls\n root->right = func(postorder, temp2, i, root->right);\n root->left = func(postorder, temp1, i, root->left);\n\n return root;\n }\n \n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int i = postorder.size() - 1;\n TreeNode* root = nullptr;\n return func(postorder, inorder, i, root);\n }\n};\n",
"memory": "79053"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int ct=0;\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if(inorder.size()==1) return new TreeNode(inorder[0]);\n \n int n = postorder.size();\n TreeNode* root = new TreeNode(postorder[n-1-ct]);\n bool flag=false;\n vector<int> l,r;\n for(auto val:inorder){\n if(val==postorder[n-1-ct]) flag=true;\n else if(flag) r.push_back(val);\n else l.push_back(val);\n }\n if(!r.empty()) if(++ct<n) root->right = buildTree(r,postorder);\n if(!l.empty()) if(++ct<n) root->left = buildTree(l,postorder);\n return root;\n }\n};",
"memory": "80294"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int i = 0;\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (inorder.size() == 0 || postorder.size() == 0) {\n return NULL;\n }\n int n = postorder.size() - i - 1; // curr pointer position\n i++;\n TreeNode* root = new TreeNode(postorder[n]); // treenode\n vector<int> l; // left inorder\n vector<int> r; // right inorder\n int i = 0;\n while (inorder[i] != postorder[n] && i < inorder.size()) {\n l.push_back(inorder[i]);\n i++;\n }\n i++;\n while (i < inorder.size()) {\n r.push_back(inorder[i]);\n i++;\n }\n root->right = buildTree(r, postorder);\n root->left = buildTree(l, postorder);\n return root;\n }\n};",
"memory": "81535"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* func(vector<int>& postorder, vector<int>& inorder, int& i, TreeNode* root) {\n if (inorder.empty()) { return nullptr; }\n\n root = new TreeNode(postorder[i--]);\n\n vector<int> temp1;\n vector<int> temp2;\n int breaker;\n\n // Find the index of the root in inorder\n for (int j = 0; j < inorder.size(); j++) {\n if (inorder[j] == root->val) {\n breaker = j;\n break;\n }\n }\n\n // Populate temp1 with elements left of the root in inorder\n for (int j = 0; j < breaker; j++) {\n temp1.push_back(inorder[j]);\n }\n\n // Populate temp2 with elements right of the root in inorder\n for (int j = breaker + 1; j < inorder.size(); j++) {\n temp2.push_back(inorder[j]);\n }\n\n // Recursive calls\n root->right = func(postorder, temp2, i, root->right);\n root->left = func(postorder, temp1, i, root->left);\n\n return root;\n }\n \n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int i = postorder.size() - 1;\n TreeNode* root = nullptr;\n return func(postorder, inorder, i, root);\n }\n};\n",
"memory": "82776"
} |
106 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= inorder.length <= 3000</code></li>
<li><code>postorder.length == inorder.length</code></li>
<li><code>-3000 <= inorder[i], postorder[i] <= 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>
| 3 | {
"code": "class Solution {\npublic:\n\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (postorder.empty()) {\n return nullptr;\n }\n\n int root_val = postorder.back(); // Root is the last element in postorder\n TreeNode* newTree = new TreeNode(root_val);\n\n if (postorder.size() == 1) {\n return newTree;\n }\n\n vector<int> inorder1;\n int i = 0;\n\n // Find the index of the root in inorder traversal\n while (inorder[i] != root_val) {\n inorder1.push_back(inorder[i]);\n i++;\n }\n\n vector<int> inorder2(inorder.begin() + i + 1, inorder.end());\n postorder.pop_back(); // Remove the root from postorder\n\n if (!inorder2.empty()) {\n vector<int> postorder2(postorder.end() - inorder2.size(), postorder.end());\n newTree->right = buildTree(inorder2, postorder2);\n postorder.erase(postorder.end() - inorder2.size(), postorder.end());\n }\n\n if (!inorder1.empty()) {\n newTree->left = buildTree(inorder1, postorder);\n }\n\n return newTree;\n }\n};\n",
"memory": "84018"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.