id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></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 vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>>res;\n if(root==NULL){\n return res;\n }\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty()){\n int s=q.size();\n vector<int>ans;\n for(int i=0;i<s;i++){\n TreeNode* temp=q.front();\n q.pop();\n if(temp->left!=NULL){\n q.push(temp->left);\n }\n if(temp->right!=NULL){\n q.push(temp->right);\n }\n ans.push_back(temp->val);\n }\n res.push_back(ans);\n }\n return res;\n }\n \n};", "memory": "15100" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
1
{ "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<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> res;\n\n queue<TreeNode*> q;\n\n if(!root) return res;\n\n q.push(root);\n while(!q.empty()){\n int size = q.size();\n vector<int> temp;\n\n // cout<<q.size()<<endl;\n for(int i=0; i<size && !q.empty(); i++){\n TreeNode* node = q.front();\n q.pop();\n\n // if(!node) return res;\n\n temp.push_back(node->val);\n\n if(node->left) q.push(node->left);\n if(node->right) q.push(node->right);\n }\n\n res.push_back(temp);\n\n }\n\n return res;\n }\n};", "memory": "15200" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
1
{ "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<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> res;\n\n if (root==nullptr){\n return {};\n }\n\n queue<pair<TreeNode*, int>> q;\n q.push({root,1});\n\n vector<int> t;\n int CurrentDepth=1;\n int count=1;\n while(!q.empty()){\n TreeNode* cur = q.front().first;\n\n if(q.front().second!=CurrentDepth){\n res.push_back(t);\n t={};\n CurrentDepth+=1;\n }\n q.pop();\n if(cur!=nullptr){\n t.push_back(cur->val);\n q.push({cur->left, CurrentDepth+1});\n q.push({cur->right, CurrentDepth+1}); \n }\n\n }\n\n return res;\n }\n};", "memory": "15200" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></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 vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>>level;\n if(root==nullptr){\n return level;\n }\n queue<TreeNode* >q;\n q.push(root);\n while(!q.empty()){\n int s=q.size();\n vector<int>v;\n for(int i=0;i<s;i++){\n TreeNode* temp=q.front();\n q.pop();\n v.push_back(temp->val);\n if(temp->left!=nullptr){\n q.push(temp->left);\n }\n if(temp->right!=nullptr){\n q.push(temp->right);\n }\n }\n level.push_back(v);\n }\n return level;\n }\n};", "memory": "15300" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></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 vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>>ans;\n if(root ==nullptr){\n return ans;\n }\n queue<TreeNode*>q;\n q.push(root);\n while(q.empty()== false){\n int n=q.size();\n vector<int>inner;\n while(n!=0){\n TreeNode *node=q.front();\n inner.push_back(node->val);\n if(node->left!=nullptr){\n q.push(node->left);\n } \n if(node->right!=nullptr){\n q.push(node->right);\n }\n q.pop();\n n--;\n }\n ans.push_back(inner);\n \n }\n return ans;\n }\n};", "memory": "15300" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>ans;\n void DFS(TreeNode*root,int h){\n if(root==nullptr) return;\n if(h>=ans.size()) ans.emplace_back();\n ans[h].push_back(root->val);\n DFS(root->left,h+1);\n DFS(root->right,h+1);\n }\n vector<vector<int>> levelOrder(TreeNode* root) {\n DFS(root,0);\n return ans;\n }\n};\n\n\n \n \n", "memory": "15400" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></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<vector<int>> ans;\n vector<vector<int>> levelOrder(TreeNode* root) {\n if(root == NULL) return ans;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n vector<int> level;\n int n = q.size();\n \n\n for(int i = 0; i < n; i++){\n TreeNode* temp;\n temp = q.front();\n q.pop();\n\n level.push_back(temp->val);\n\n if(temp->left != NULL) q.push(temp->left);\n if(temp->right != NULL) q.push(temp->right);\n }\n ans.push_back(level);\n\n \n }\n return ans;\n }\n};", "memory": "15400" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></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<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n levelorder(root,ans,0);\n return ans;\n }\n\n void levelorder(TreeNode* root,vector<vector<int>>& ans,int level){\n if(root!=nullptr){\n if(ans.size()==level){\n vector<int> s;\n ans.push_back(s);\n }\n ans[level].push_back(root->val);\n levelorder(root->left,ans,level+1);\n levelorder(root->right,ans,level+1);\n }\n }\n};", "memory": "15500" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></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<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n levelorder(root,ans,0);\n return ans;\n }\n\n void levelorder(TreeNode* root,vector<vector<int>>& ans,int level){\n if(root!=nullptr){\n if(ans.size()==level){\n vector<int> s;\n ans.push_back(s);\n }\n ans[level].push_back(root->val);\n levelorder(root->left,ans,level+1);\n levelorder(root->right,ans,level+1);\n }\n }\n};", "memory": "15500" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></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<vector<int>> levelOrder(TreeNode *root)\n {\n vector<vector<int>> level;\n bfs(root, level);\n return level;\n }\n\n void bfs(TreeNode *root, vector<vector<int>> &level)\n { \n if (root)\n {\n queue<vector<TreeNode *>> order;\n vector<TreeNode *> first;\n first.push_back(root);\n order.push(first);\n vector<int> firstint;\n firstint.push_back(root->val);\n level.push_back(firstint);\n while (!order.empty())\n {\n vector<int> nextlevel;\n vector<TreeNode *> nextroots;\n vector<TreeNode *> prevlevel = order.front();\n for (auto node : prevlevel)\n {\n if (node)\n {\n if (node->left)\n {\n nextlevel.push_back(node->left->val);\n nextroots.push_back(node->left);\n }\n if (node->right)\n {\n nextlevel.push_back(node->right->val);\n nextroots.push_back(node->right);\n }\n }\n }\n order.pop();\n if (nextroots.size()>0)\n {\n order.push(nextroots);\n level.push_back(nextlevel);\n }\n }\n }\n \n }\n\n};", "memory": "15600" }
102
<p>Given the <code>root</code> of a binary tree, return <em>the level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[9,20],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></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<vector<int>> levelOrder(TreeNode *root)\n {\n vector<vector<int>> level;\n bfs(root, level);\n return level;\n }\n\n void bfs(TreeNode *root, vector<vector<int>> &level)\n { \n if (root)\n {\n queue<vector<TreeNode *>> order;\n vector<TreeNode *> first;\n first.push_back(root);\n order.push(first);\n vector<int> firstint;\n firstint.push_back(root->val);\n level.push_back(firstint);\n while (!order.empty())\n {\n vector<int> nextlevel;\n vector<TreeNode *> nextroots;\n vector<TreeNode *> prevlevel = order.front();\n for (auto node : prevlevel)\n {\n if (node)\n {\n if (node->left)\n {\n nextlevel.push_back(node->left->val);\n nextroots.push_back(node->left);\n }\n if (node->right)\n {\n nextlevel.push_back(node->right->val);\n nextroots.push_back(node->right);\n }\n }\n }\n order.pop();\n if (nextroots.size()>0)\n {\n order.push(nextroots);\n level.push_back(nextlevel);\n }\n }\n }\n \n }\n\n};", "memory": "15600" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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),\n * right(right) {}\n * };\n */\n#include <queue>\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if (root == nullptr)\n return ans;\n int level = 0;\n queue<TreeNode*> q;\n q.push(root);\n TreeNode* temp = q.front();\n\n while (!q.empty()) {\n int size = q.size();\n\n vector<int> v;\n for (int i = 0; i < size; i++) {\n temp = q.front();\n q.pop();\n v.push_back(temp->val);\n if (temp->left != nullptr)\n q.push(temp->left);\n\n if (temp->right != nullptr)\n q.push(temp->right);\n }\n if (level % 2 != 0)\n reverse(v.begin(), v.end());\n\n level++;\n ans.push_back(v);\n }\n root->left = root->right = nullptr;\n return ans;\n }\n};", "memory": "10300" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n if(!root) return {};\n vector<vector<int>> v;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int i = q.size(); vector<int> v1;\n for(int j=0;j<i;j++){\n TreeNode* r = q.front(); q.pop();\n if(r==NULL) continue;\n v1.push_back(r->val);\n \n if(r->left!=NULL) q.push(r->left);\n if(r->right!=NULL) q.push(r->right);\n }\n v.push_back(v1);\n }\n for(int i=1;i<v.size();i+=2){\n reverse(v[i].begin(), v[i].end());\n }\n root->left = root->right = NULL;\n root=NULL; free(root);\n return v;\n }\n};", "memory": "10400" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> res;\n \n if (root == nullptr) {\n return res;\n }\n \n queue<TreeNode*> Q;\n Q.push(root);\n \n while (!Q.empty()) {\n int sz = Q.size(), iter = res.size();\n res.push_back({});\n \n for (int i = 0; i < sz; ++i) {\n TreeNode* node = Q.front();\n Q.pop();\n \n res.back().push_back(node->val);\n \n if (node->left) { \n Q.push(node->left);\n }\n \n if (node->right != nullptr) { \n Q.push(node->right);\n }\n }\n \n if (iter % 2 == 1) {\n reverse(res.back().begin(), res.back().end());\n }\n }\n \n return res;\n }\n};", "memory": "12900" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if (root == nullptr) return ans;\n deque<TreeNode*> dq; // :P\n dq.push_back(root);\n bool front = true; // :D\n queue<TreeNode*> lvlq;\n while (!dq.empty()) {\n ans.push_back(vector<int>());\n auto level = ans.end() - 1;\n while (!dq.empty()) {\n TreeNode* bepis = front ? dq.front() : dq.back();\n if (front) dq.pop_front(); else dq.pop_back();\n (*level).push_back(bepis->val);\n if (front) {\n if (bepis->left != nullptr) lvlq.push(bepis->left);\n if (bepis->right != nullptr) lvlq.push(bepis->right);\n } else {\n if (bepis->right != nullptr) lvlq.push(bepis->right);\n if (bepis->left != nullptr) lvlq.push(bepis->left);\n }\n }\n while (!lvlq.empty()) {\n if (front) dq.push_back(lvlq.front());\n else dq.push_front(lvlq.front());\n lvlq.pop();\n }\n front ^= 1;\n }\n return ans;\n }\n};", "memory": "13000" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if (!root)\n return ans;\n\n queue<TreeNode*> q;\n bool LtrDir = true;\n\n q.push(root);\n\n while (!q.empty()) {\n int width = q.size();\n vector<int> result(width);\n for (int i = 0; i < width; i++) {\n TreeNode* front = q.front();\n q.pop();\n\n int index = LtrDir ? i : width - i - 1;\n\n result[index] = front->val;\n if (front->left)\n q.push(front->left);\n\n if (front->right)\n q.push(front->right);\n }\n LtrDir = !LtrDir;\n ans.push_back(result);\n }\n\n return ans;\n }\n};", "memory": "13000" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> result;\n if(root==NULL)return result;\n\n queue<TreeNode*>q;\n q.push(root);\n bool L2R=true;\n\n while(!q.empty()){\n int size=q.size();\n vector<int>row(size);\n for(int i=0;i<size;i++){\n TreeNode* node=q.front();\n q.pop();\n\n int index=(L2R)?i:(size-1-i);\n\n row[index]=node->val;\n\n if(node->left){\n q.push(node->left);\n }\n if(node->right){\n q.push(node->right);\n }\n }\n L2R=!L2R;\n result.push_back(row);\n }\n return result;\n }\n};", "memory": "13100" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n void helper(TreeNode* root, vector<vector<int>>& ans) {\n if (!root) return;\n\n queue<TreeNode*> q;\n q.push(root); \n bool leftToRight = true; \n\n while (!q.empty()) {\n int len = q.size();\n vector<int> temp(len);\n\n for (int i = 0; i < len; ++i) {\n TreeNode* node = q.front();\n q.pop();\n\n if (leftToRight) {\n temp[i] = node->val;\n } else {\n temp[len - 1 - i] = node->val;\n }\n\n \n if (node->left) q.push(node->left);\n if (node->right) q.push(node->right);\n }\n\n ans.push_back(temp);\n leftToRight = !leftToRight; \n }\n }\n\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n helper(root, ans);\n return ans;\n }\n};", "memory": "13100" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> result;\n if(root == NULL){\n return result;\n }\n queue<TreeNode*> q;\n bool lefttoRight = true;\n q.push(root);\n while(!q.empty()){\n int size = q.size();\n vector<int> temp(size);\n for(int i=0; i<size; i++){\n root = q.front();\n q.pop();\n int index = lefttoRight ? i : size - 1 - i;\n temp[index] = root->val;\n if(root->left){\n q.push(root->left);\n }\n if(root->right){\n q.push(root->right);\n }\n }\n lefttoRight = !lefttoRight;\n result.push_back(temp);\n }\n return result;\n }\n};", "memory": "13200" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n if (!root) {\n return {};\n }\n bool left = true;\n std::vector<std::vector<int>> res;\n\n std::queue<TreeNode*> q;\n q.push(root);\n\n while (!q.empty()) {\n int s = q.size();\n\n std::vector<int> tmp;\n while (s--) {\n TreeNode* node = q.front();\n q.pop();\n tmp.push_back(node->val);\n\n if (node->left) {\n q.push(node->left);\n }\n if (node->right) {\n q.push(node->right);\n }\n }\n\n if (!left) {\n std::reverse(tmp.begin(), tmp.end());\n }\n\n res.push_back(tmp);\n\n left = !left;\n }\n\n return res;\n }\n};", "memory": "13200" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n if(!root)\n {\n return{};\n }\n queue<TreeNode*> q;\n vector<vector<int>> fin;\n q.push(root);\n int level=0;\n while(!q.empty())\n {\n int s=q.size();\n vector<int> ans;\n for(int i=0;i<s;i++)\n {\n TreeNode* node=q.front();\n q.pop();\n ans.push_back(node->val);\n if(node->left)\n {\n q.push(node->left);\n }\n if(node->right)\n {\n q.push(node->right);\n }\n }\n if(level%2==1)\n {\n reverse(ans.begin(),ans.end());\n }\n fin.push_back(ans);\n level++;\n }\n\n \n return fin;\n \n }\n};", "memory": "13300" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>>res;\n queue<TreeNode*>q;\n if(root==nullptr) return res;\n q.push(root);\n int flag = 0;\n while(!q.empty()){\n int level_size = q.size();\n vector<int> curr_lvl;\n for(int i =0;i<level_size;i++){\n TreeNode* curr = q.front();\n q.pop();\n curr_lvl.push_back(curr->val);\n if(curr->left!=nullptr) q.push(curr->left);\n if(curr->right!=nullptr) q.push(curr->right);\n }\n if(flag%2!=0){\n reverse(curr_lvl.begin(), curr_lvl.end());\n }\n res.push_back(curr_lvl);\n flag =!flag;\n }\n return res;\n }\n};", "memory": "13300" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
1
{ "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<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> result; // To store the result\n if (!root) return result; // Return empty result if the root is null\n \n queue<TreeNode*> q; // Queue for level order traversal\n q.push(root); // Start with the root node\n bool leftToRight = true; // Flag to control zigzag direction\n \n while (!q.empty()) {\n int levelSize = q.size(); // Number of nodes in the current level\n vector<int> currentLevel; // To store the nodes of the current level\n \n // Process all nodes in the current level\n for (int i = 0; i < levelSize; ++i) {\n TreeNode* currentNode = q.front();\n q.pop();\n currentLevel.push_back(currentNode->val); // Add node value to current level\n \n // Add the left and right children to the queue\n if (currentNode->left) q.push(currentNode->left);\n if (currentNode->right) q.push(currentNode->right);\n }\n \n // If we are traversing right to left, reverse the current level\n if (!leftToRight) {\n reverse(currentLevel.begin(), currentLevel.end());\n }\n \n result.push_back(currentLevel); // Add current level to result\n leftToRight = !leftToRight; // Toggle the direction for the next level\n }\n \n return result;\n }\n};", "memory": "13400" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
1
{ "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 traversal(TreeNode* root,int level,vector<vector<int>> &ans){\n if(root!=NULL){\n ans[level].push_back(root->val);\n }\n else return;\n if(root->left) traversal(root->left,level+1,ans);\n if(root->right) traversal(root->right,level+1,ans);\n }\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans1(21),ans;\n \n traversal(root,0,ans1);\n for(int i=0;i<21;i++){\n if(ans1[i].size()>0){\n if(i%2==0){\n ans.push_back(ans1[i]);\n }\n else{\n reverse(ans1[i].begin(),ans1[i].end());\n ans.push_back(ans1[i]);\n }\n }\n else break;\n }\n\n return ans;\n }\n};", "memory": "13400" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>>ans;\n queue<TreeNode *>q;\n q.push(root);\n if(root==NULL)return {};\n q.push(NULL);\n vector<int>level;\n int l=0;\n while(!q.empty()){\n TreeNode *top=q.front();\n q.pop();\n \n if(top==NULL){\n\n if(!q.empty()){\n q.push(NULL);\n }\n if(l%2){\n reverse(level.begin(), level.end());\n }\n ans.push_back(level);\n level.clear();\n l++;\n }else{\n level.push_back(top->val);\n if(top->left){\n q.push(top->left);\n }\n if(top->right){\n q.push(top->right);\n }\n }\n }\n return ans;\n }\n};", "memory": "13500" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n if (root == nullptr)\n return {};\n\n stack<TreeNode*> st1; // Stack for current level\n stack<TreeNode*> st2; // Stack for next level\n st1.push(root);\n vector<vector<int>> ans;\n vector<int> a;\n\n while (!st1.empty() || !st2.empty()) {\n if (!st1.empty()) {\n while (!st1.empty()) {\n TreeNode* temp = st1.top();\n st1.pop();\n a.push_back(temp->val);\n if (temp->left) {\n st2.push(temp->left);\n }\n if (temp->right) {\n st2.push(temp->right);\n }\n }\n } else {\n while (!st2.empty()) {\n TreeNode* temp = st2.top();\n st2.pop();\n a.push_back(temp->val);\n if (temp->right) {\n st1.push(temp->right);\n }\n if (temp->left) {\n st1.push(temp->left);\n }\n }\n }\n\n ans.push_back(a);\n a.clear();\n }\n return ans;\n }\n};\n", "memory": "13500" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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// vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n// vector<vector<int>>ans;\n// if(root==NULL){\n// return ans;\n// }\n// queue<TreeNode*>q;\n// q.push(root);\n// bool leftToRight=true;\n// while(!q.empty()){\n// vector<int>res;\n// int size=q.size();\n// for(int i=0;i<size;i++){\n// TreeNode*node=q.front();\n// q.pop();\n// if(leftToRight){\n// res.push_back(node->val);\n// }\n// else{\n// res.insert(res.begin(),node->val);\n// }\n// if(node->left ){\n// q.push(node->left);\n// }\n// if(node->right){\n// q.push(node->right);\n// }\n \n// }\n \n// ans.push_back(res);\n// leftToRight=!leftToRight;\n \n// }\n// return ans;\n \n// }\n// };\n\n\n\n\n\n\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if (root == NULL) {\n return ans;\n }\n \n queue<TreeNode*> q;\n q.push(root);\n bool leftToRight = true;\n \n while (!q.empty()) {\n deque<int> res; \n int size = q.size();\n \n for (int i = 0; i < size; i++) {\n TreeNode* node = q.front();\n q.pop();\n \n if (leftToRight) {\n res.push_back(node->val); \n } else {\n res.push_front(node->val); \n }\n\n if (node->left) {\n q.push(node->left);\n }\n \n if (node->right) {\n q.push(node->right);\n }\n }\n\n ans.push_back(vector<int>(res.begin(), res.end())); \n leftToRight = !leftToRight; \n }\n \n return ans;\n }\n};\n", "memory": "13600" }
103
<p>Given the <code>root</code> of a binary tree, return <em>the zigzag level order traversal of its nodes&#39; values</em>. (i.e., from left to right, then right to left for the next level and alternate between).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> [[3],[20,9],[15,7]] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> [[1]] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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<vector<int>> zigzagLevelOrder(TreeNode* root) {\n if (root==nullptr) return vector<vector<int>>();\n deque<TreeNode*> node_queue;\n vector<vector<int>> ans;\n node_queue.push_back(root);\n node_queue.push_back(nullptr);\n deque<int> level_list;\n bool is_order_left = true;\n while(!node_queue.empty()) {\n TreeNode* temp = node_queue.front();\n node_queue.pop_front();\n if(temp!=nullptr) {\n if(is_order_left) {\n level_list.push_back(temp->val);\n } else {\n level_list.push_front(temp->val);\n }\n if (temp->left) node_queue.push_back(temp->left);\n if (temp->right) node_queue.push_back(temp->right);\n } else {\n ans.push_back(vector<int>{level_list.begin(),level_list.end()});\n level_list.clear();\n if(!node_queue.empty()) node_queue.push_back(nullptr);\n is_order_left = !is_order_left;\n }\n }\n return ans; \n }\n};", "memory": "13600" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "\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 */\n\nclass Solution {\npublic:\n int maxDepth(TreeNode* root) {\n stack<TreeNode*> s;\n int mx=0;\n if(root==nullptr)\n return mx;\n s.push(root);\n while(!s.empty())\n {\n TreeNode* nd=s.top();\n\n if(nd->left!=nullptr)\n {\n s.push(nd->left);\n nd->left=nullptr;\n }\n else\n {\n if(nd->right!=nullptr)\n {\n s.push(nd->right);\n nd->right=nullptr;\n }\n else\n {\n if(s.size()>mx)\n mx=s.size();\n s.pop();\n }\n }\n }\n return mx;\n\n \n }\n};", "memory": "12200" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root) {\n if(root==NULL)\n {\n return 0;\n }\n \n int ans=max(maxDepth(root->left),maxDepth(root->right))+1;\n root->left=NULL;\n root->right=NULL;\n return ans;\n\n }\n};", "memory": "12300" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int helper(TreeNode *root) {\n if (!root) return 0;\n auto l = helper(root->left),\n r = helper(root->right);\n root->left = root->right = nullptr;\n return 1 + max(l,r);\n }\n \n int maxDepth(TreeNode* root) {\n return helper(root);\n }\n};", "memory": "12400" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int helper(TreeNode *root) {\n if (!root) return 0;\n auto l = helper(root->left),\n r = helper(root->right);\n root->left = root->right = nullptr;\n return 1 + max(l,r);\n }\n \n int maxDepth(TreeNode* root) {\n return helper(root);\n }\n};", "memory": "12500" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 inline int helper(TreeNode *root) {\n if (!root) return 0;\n auto l = helper(root->left),\n r = helper(root->right);\n root->left = root->right = nullptr;\n return 1 + max(l,r);\n }\n \n int maxDepth(TreeNode* root) {\n return helper(root);\n }\n};", "memory": "12500" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root) {\n cin.tie(0); cout.tie(0);\n ios::sync_with_stdio(0);\n\n if (!root) return 0;\n int ans = max(maxDepth(root->left), maxDepth(root->right)) + 1;\n root->left = root->right = nullptr;\n return ans;\n }\n};", "memory": "12600" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root)\n {\n if (root == NULL)\n {\n return 0;\n }\n \n return std::max(\n 1 + maxDepth(root->left),\n 1 + maxDepth(root->right)\n );\n }\n};", "memory": "17300" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root) {\n if(root==nullptr) return 0;\n int left = maxDepth(root->left);\n int right = maxDepth(root->right);\n return 1+max(left,right);\n }\n};", "memory": "17400" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root) {\n // count level\n // recursion\n int left_depth = 0;\n int right_depth = 0;\n\n if (!root)\n return 0;\n\n return 1 + max(maxDepth(root->left), maxDepth(root->right));\n\n }\n};", "memory": "17400" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 {\n int depth(TreeNode *p){\n if(!p)return 0;\n return max(1+depth(p->left),1+depth(p->right));\n }\npublic:\n int maxDepth(TreeNode* root) {\n return depth(root); \n }\n};", "memory": "17500" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 {\n\npublic:\n int maxDepth(TreeNode* root) {\n \n if(root==NULL) return 0;\n \n int leftDepth = maxDepth(root->left);\n int rightDepth = maxDepth(root->right);\n return max(leftDepth, rightDepth)+1;\n }\n};", "memory": "17500" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root) {\n if(root==nullptr)\n return 0;\n if(root->left==nullptr && root->right==nullptr)\n return 1;\n int left=0,right=0;\n if(root->left!=nullptr)\n left = maxDepth(root->left);\n if(root->right!=nullptr)\n right = maxDepth(root->right);\n return max(left,right) + 1;\n }\n};", "memory": "17600" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root) {\n if(!root) return 0;\n int maxLeft = maxDepth(root->left);\n int maxRight = maxDepth(root->right);\n return max(maxLeft, maxRight)+1;\n }\n};", "memory": "17600" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root) {\n if(root == nullptr) {\n return 0;\n }\n\n return max(maxDepth(root->left), maxDepth(root->right))+1;\n\n }\n};\n/*\n\n int left = traverse(root->left, 1);\n int right = traverse(root->right, 1);\n\n if(left >= right) {\n return left;\n }\n else\n return right;\n }\n\n int traverse(TreeNode* root, int depth) {\n if(root == nullptr)\n return depth;\n \n int left = traverse(root->left, depth++);\n int right = traverse(root->right, depth++);\n\n cout << \"Left: \" << left << endl;\n cout << \"Right: \" << right << endl;\n\n if(left >= right) {\n return left;\n }\n else\n return right;\n }\n};\n\n*/", "memory": "17700" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int solver(TreeNode* root) {\n if(root == NULL) return 0;\n return 1 + max(solver(root -> left), solver(root -> right));\n }\n\n int maxDepth(TreeNode* root) {\n return solver(root);\n }\n};", "memory": "17700" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root) {\n if (!root) return 0;\n auto l_d = maxDepth(root->left);\n auto r_d = maxDepth(root->right);\n return 1 + std::max(l_d, r_d);\n }\n};", "memory": "17800" }
104
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> <p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg" style="width: 400px; height: 277px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 3 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,2] <strong>Output:</strong> 2 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></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 int maxDepth(TreeNode* root) {\n if(!root) return 0;\n int maxLeft = maxDepth(root->left);\n int maxRight = maxDepth(root->right);\n return max(maxLeft, maxRight)+1;\n \n }\n};", "memory": "17800" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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>
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 void build(TreeNode* root, int*& pointer1, int*& pointer2, vector<int>::iterator begin, vector<int>::iterator end){\n bool f1 = end-begin>1;\n bool h2 = pointer1 != pointer2;\n auto i = find(begin, end, *pointer1);\n if(end-begin>1 && pointer1 != pointer2){\n auto find1 = find(begin, end, *(pointer1 + 1));\n if(find1 != end && i > find1){\n root->left = new TreeNode(*(++pointer1));\n build(root->left, pointer1, pointer2, begin, i);\n }\n }\n bool f = end-begin>1;\n bool h = pointer1 != pointer2;\n if(end-begin>1 && pointer1 != pointer2){\n auto find1 = find(begin, end, *(pointer1 + 1));\n if(find1 != end && i < find1){\n root->right = new TreeNode(*(++pointer1));\n build(root->right, pointer1, pointer2, i+1, end);\n }\n }\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int* pointer1 = &preorder.front();\n int* pointer2 = &preorder.back();\n TreeNode* root = new TreeNode(*pointer1);\n build(root, pointer1, pointer2, inorder.begin(), inorder.end());\n return root;\n }\n};", "memory": "27455" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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>
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 void build(TreeNode* root, vector<int>& preorder, vector<int>::iterator begin, vector<int>::iterator end) {\n auto i = find(begin, end, preorder.front());\n if(end-begin>1 && preorder.size()>1){\n auto find1 = find(begin, end, preorder.at(1));\n if(find1 != end && i-begin > find1-begin){\n preorder.erase(preorder.begin());\n root->left = new TreeNode(preorder.front());\n build(root->left, preorder, begin, i);\n }\n }\n if(end-begin>1 && preorder.size()>1){\n auto find1 = find(begin, end, preorder.at(1));\n if(find1 != end && i-begin < find1-begin){\n preorder.erase(preorder.begin());\n root->right = new TreeNode(preorder.front());\n build(root->right, preorder, i+1, end);\n }\n }\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* root = new TreeNode(preorder.front());\n build(root, preorder, inorder.begin(), inorder.end());\n return root;\n }\n};", "memory": "27455" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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\n vector<int> po,io;\n int n;\n bool b[6001];\n unordered_map<int,int> pos;\n\npublic:\n\n TreeNode* left(int l, int r){\n TreeNode *ret;\n ret=new TreeNode();\n if (l==r){\n ret->val=po[l];\n b[3000+po[l]]=1;\n }else{\n int k=0;\n for (int i=0;i<n;i++) {\n if (io[i]==po[l]) break;\n if (!b[3000+io[i]]) k++;\n }\n b[3000+po[l]]=1;\n ret->val=po[l];\n if (k) ret->left=left(l+1,l+k);\n if (l+k+1<=r) ret->right=right(l+k+1,r);\n }\n return ret;\n } \n TreeNode* right(int l, int r){\n TreeNode *ret;\n ret=new TreeNode();\n if (l==r){\n ret->val=po[l];\n b[3000+po[l]]=1;\n }else{\n int k=0;\n for (int i=0;i<n;i++) {\n if (io[i]==po[l]) break;\n if (!b[3000+io[i]]) k++;\n }\n b[3000+po[l]]=1;\n ret->val=po[l];\n if (k) ret->left=left(l+1,l+k);\n if (l+k+1<=r) ret->right=right(l+k+1,r);\n }\n return ret;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n n=preorder.size();\n for (int i=0;i<n;i++){\n pos[inorder[i]]=i;\n po.push_back(preorder[i]);\n io.push_back(inorder[i]);\n } \n for (int i=0;i<6001;i++) b[i]=0;\n TreeNode *head;\n head=new TreeNode();\n head->val=preorder[0];\n b[3000+preorder[0]]=1;\n if (n<2) return head;\n int k=0;\n for (int i=0;i<pos[preorder[0]];i++){\n if (!b[3000+inorder[i]])k++;\n } \n if (k!=0) head->left=left(1,k);\n if (k+1<=n-1) head->right=right(k+1,n-1);\n\n return head;\n }\n};", "memory": "29766" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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\n vector<int> po,io;\n int n;\n bool b[6001];\n unordered_map<int,int> pos;\n\npublic:\n\n TreeNode* left(int l, int r){\n TreeNode *ret;\n ret=new TreeNode();\n if (l==r){\n ret->val=po[l];\n b[3000+po[l]]=1;\n }else{\n int k=0;\n int lim=pos[po[l]];\n for (int i=0;i<lim;i++) {\n if (!b[3000+io[i]]) k++;\n }\n b[3000+po[l]]=1;\n ret->val=po[l];\n if (k) ret->left=left(l+1,l+k);\n if (l+k+1<=r) ret->right=right(l+k+1,r);\n }\n return ret;\n } \n TreeNode* right(int l, int r){\n TreeNode *ret;\n ret=new TreeNode();\n if (l==r){\n ret->val=po[l];\n b[3000+po[l]]=1;\n }else{\n int k=0;\n int lim=pos[po[l]];\n for (int i=0;i<lim;i++) {\n if (!b[3000+io[i]]) k++;\n }\n b[3000+po[l]]=1;\n ret->val=po[l];\n if (k) ret->left=left(l+1,l+k);\n if (l+k+1<=r) ret->right=right(l+k+1,r);\n }\n return ret;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n n=preorder.size();\n for (int i=0;i<n;i++){\n pos[inorder[i]]=i;\n po.push_back(preorder[i]);\n io.push_back(inorder[i]);\n } \n for (int i=0;i<6001;i++) b[i]=0;\n TreeNode *head;\n head=new TreeNode();\n head->val=preorder[0];\n b[3000+preorder[0]]=1;\n if (n<2) return head;\n int k=0;\n for (int i=0;i<pos[preorder[0]];i++){\n if (!b[3000+inorder[i]])k++;\n } \n if (k!=0) head->left=left(1,k);\n if (k+1<=n-1) head->right=right(k+1,n-1);\n\n return head;\n }\n};", "memory": "29766" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 TreeNode* buildTreeHelper(int& i, int lo, int hi, vector<int>& preorder, vector<int>& inorder, vector<int>& inorderIndex) {\n if (lo > hi) return nullptr;\n\n TreeNode* root = new TreeNode(preorder[i]);\n\n int j = inorderIndex[preorder[i++] + 3000];\n root->left = buildTreeHelper(i, lo, j - 1, preorder, inorder, inorderIndex);\n root->right = buildTreeHelper(i, j + 1, hi, preorder, inorder, inorderIndex);\n\n return root;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n vector<int> inorderIndex(6001);\n int n = preorder.size();\n for (int i = 0; i < n; i++)\n inorderIndex[inorder[i] + 3000] = i;\n\n int i = 0;\n return buildTreeHelper(i, 0, n - 1, preorder, inorder, inorderIndex);\n }\n};", "memory": "32078" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 TreeNode* construct(int inStart, int inEnd, int preStart, int preEnd,\n vector<int>& inorder, vector<int>& preorder, vector<int>& inOrderIdx) {\n if (inStart > inEnd || preStart > preEnd) {\n return nullptr;\n }\n // root is at preStart\n TreeNode* root = new TreeNode(preorder[preStart]);\n int idx = inOrderIdx[root->val + 3000];\n int rightLength = inEnd - idx;\n // elements to left of root in order are left subtree\n root->left = construct(inStart, idx - 1, preStart + 1,\n preEnd - rightLength, inorder, preorder, inOrderIdx);\n // elements to right in order are right subtree\n root->right = construct(idx + 1, inEnd, preEnd - rightLength + 1,\n preEnd, inorder, preorder, inOrderIdx);\n return root;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\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, preorder, inOrderIdx); \n }\n};", "memory": "32078" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 std::deque<int> preorder_dq(preorder.begin(), preorder.end());\n\n return buildTree_dq(preorder_dq, inorder);\n }\n\n TreeNode* buildTree_dq(std::deque<int>& preorder, std::vector<int>& inorder) {\n if (inorder.empty()) {\n return nullptr;\n }\n\n TreeNode *n = new TreeNode(preorder[0]);\n auto iter = std::find(inorder.begin(), inorder.end(), preorder[0]);\n\n preorder.pop_front();\n\n std::vector<int> new_in_left(inorder.begin(), iter);\n n->left = buildTree_dq(preorder, new_in_left);\n std::vector<int> new_in_right(iter+1, inorder.end());\n n->right = buildTree_dq(preorder, new_in_right);\n\n return n;\n }\n};", "memory": "34389" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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\n int rootVal = preorder.front();\n preorder.erase(preorder.begin());\n\n auto rootIdx = find(inorder.begin(), inorder.end(), rootVal);\n\n TreeNode* root = new TreeNode(rootVal);\n\n vector<int> leftInorder = vector<int>(inorder.begin(), rootIdx);\n vector<int> rightInorder = vector<int>(rootIdx+1, inorder.end());\n\n root->left = buildTree(preorder, leftInorder);\n root->right = buildTree(preorder, rightInorder);\n return root;\n }\n};", "memory": "36700" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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* root = new TreeNode();\n void build(TreeNode* root, vector<int>& preorder, vector<int>& inorder) {\n auto i = find(inorder.begin(), inorder.end(), preorder.at(0));\n if(inorder.size()>1 && preorder.size()>1 && find(inorder.begin(), inorder.end(), preorder.at(1)) != inorder.end() && i-inorder.begin() > find(inorder.begin(), inorder.end(), preorder.at(1))-inorder.begin()){\n preorder.erase(preorder.begin());\n root->left = new TreeNode(preorder.at(0));\n vector<int> lsub_inorder(inorder.begin(), i);\n build(root->left, preorder, lsub_inorder);\n }\n if(inorder.size()>1 && preorder.size()>1 && find(inorder.begin(), inorder.end(), preorder.at(1)) != inorder.end() && i-inorder.begin() < find(inorder.begin(), inorder.end(), preorder.at(1))-inorder.begin()){\n preorder.erase(preorder.begin());\n root->right = new TreeNode(preorder.at(0));\n vector<int> rsub_inorder(i+1, inorder.end()); \n build(root->right, preorder, rsub_inorder);\n }\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n root->val = preorder.at(0);\n build(root, preorder, inorder);\n return root;\n }\n};", "memory": "39011" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if (inorder.size() == 1) return new TreeNode(inorder[0]);\n if (inorder.size() == 0) return nullptr;\n\n int i = 0;\n int middle = -1;\n for(i = 0; i < preorder.size(); i++)\n {\n for(int j = 0; j < inorder.size(); j++)\n {\n if (inorder[j] == preorder[i])\n {\n middle = j;\n break;\n }\n }\n if (middle != -1) break;\n }\n\n auto root = new TreeNode(preorder[i]);\n preorder.erase(preorder.begin(), preorder.begin() + i);\n\n vector<int> left(inorder.begin(), inorder.begin() + middle);\n vector<int> right(inorder.begin() + middle + 1, inorder.end());\n\n root->left = buildTree(preorder, left);\n root->right = buildTree(preorder, right);\n\n return root;\n }\n};", "memory": "41323" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 {\nprivate:\n // TreeNode* helper(int preStart, int inStart, int inEnd, vector<int>& pre, vector<int>& in) {\n // if (preStart > pre.size()-1 || inStart > inEnd) return nullptr;\n // TreeNode *node = new TreeNode(pre[preStart]);\n // if (inEnd == inStart) return node;\n // int idx = 0;\n // for (int i = inStart; i <= inEnd; i++) {\n // if (in[i] == pre[preStart]) {\n // idx = i;\n // break;\n // }\n // }\n // node->left = helper(preStart+1, inStart, idx-1, pre, in);\n // node->right = helper(preStart+idx-inStart+1, idx+1, inEnd, pre, in);\n // return node;\n // }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n // return helper(0, 0,inorder.size()-1, preorder, inorder);\n \n if (!inorder.empty()) {\n int idx = inorder.size()-1;\n for (int i = 0; i < inorder.size(); i++) {\n if (inorder[i] == preorder[0]) {\n idx = i;\n break;\n }\n }\n \n TreeNode *root = new TreeNode(preorder[0]);\n preorder.erase(preorder.begin());\n vector<int> split;\n if (idx > 0) {\n split = vector(inorder.begin(), inorder.begin()+idx);\n root->left = buildTree(preorder, split);\n }\n if (idx < inorder.size() - 1) {\n split = vector(inorder.begin()+idx+1, inorder.end());\n root->right = buildTree(preorder, split);\n }\n return root;\n }\n return NULL;\n\n\n // if (!inorder.empty()) {\n // int ind = find(inorder.begin(), inorder.end(), preorder[0]) - inorder.begin();\n // TreeNode* root = new TreeNode(preorder[0]);\n // preorder.erase(preorder.begin());\n // vector<int> leftInorder(inorder.begin(), inorder.begin() + ind);\n // vector<int> rightInorder(inorder.begin() + ind + 1, inorder.end());\n // root->left = buildTree(preorder, leftInorder);\n // root->right = buildTree(preorder, rightInorder);\n // return root;\n // }\n // return nullptr;\n }\n};", "memory": "41323" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 deque<int> preorderQueue(preorder.begin(), preorder.end());\n return build(preorderQueue, inorder); \n }\n\nprivate:\n TreeNode* build(deque<int>& preorder, vector<int>& inorder) {\n if (!inorder.empty()) {\n int val = preorder.front();\n preorder.pop_front();\n auto it = find(inorder.begin(), inorder.end(), val);\n int idx = it - inorder.begin();\n\n TreeNode* root = new TreeNode(val);\n vector<int> leftInorder(inorder.begin(), inorder.begin() + idx);\n vector<int> rightInorder(inorder.begin() + idx + 1, inorder.end());\n\n root->left = build(preorder, leftInorder);\n root->right = build(preorder, rightInorder);\n\n return root;\n }\n\n return nullptr;\n } \n};", "memory": "43634" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 {\nprivate:\n // TreeNode* helper(int preStart, int inStart, int inEnd, vector<int>& pre, vector<int>& in) {\n // if (preStart > pre.size()-1 || inStart > inEnd) return nullptr;\n // TreeNode *node = new TreeNode(pre[preStart]);\n // if (inEnd == inStart) return node;\n // int idx = 0;\n // for (int i = inStart; i <= inEnd; i++) {\n // if (in[i] == pre[preStart]) {\n // idx = i;\n // break;\n // }\n // }\n // node->left = helper(preStart+1, inStart, idx-1, pre, in);\n // node->right = helper(preStart+idx-inStart+1, idx+1, inEnd, pre, in);\n // return node;\n // }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n // return helper(0, 0,inorder.size()-1, preorder, inorder);\n // int idx = 0;\n // if (!inorder.empty()) {\n // for (int i = 0; i < inorder.size(); i++) {\n // if (inorder[i] == preorder[0]) {\n // idx = i;\n // break;\n // }\n // }\n // preorder.erase(preorder.begin());\n // TreeNode *root = new TreeNode(inorder[idx]);\n // vector<int> split;\n // if (idx > 0) {\n // split = vector(inorder.begin(), inorder.begin()+idx-1);\n // root->left = buildTree(preorder, split);\n // preorder.erase(preorder.begin());\n // }\n // if (idx < inorder.size() - 1) {\n // split = vector(inorder.begin()+idx+1, inorder.end());\n // root->right = buildTree(preorder, split);\n // }\n // return root;\n // }\n // return NULL;\n if (!inorder.empty()) {\n // Find the index of the root in the inorder list\n int ind = find(inorder.begin(), inorder.end(), preorder[0]) - inorder.begin();\n // Create the root node\n TreeNode* root = new TreeNode(preorder[0]);\n // Remove the first element from preorder\n preorder.erase(preorder.begin());\n // Recursively build the left and right subtrees\n vector<int> leftInorder(inorder.begin(), inorder.begin() + ind);\n vector<int> rightInorder(inorder.begin() + ind + 1, inorder.end());\n root->left = buildTree(preorder, leftInorder);\n root->right = buildTree(preorder, rightInorder);\n return root;\n }\n return nullptr;\n }\n};", "memory": "43634" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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(deque<int>& preorder, vector<int>& inorder) {\n if (inorder.empty()) return nullptr;\n\n int root_val = preorder.front();\n preorder.pop_front();\n\n int idx = 0;\n while (inorder[idx] != root_val) {\n idx++;\n }\n\n TreeNode* root = new TreeNode(root_val);\n\n vector<int> left_inorder(inorder.begin(), inorder.begin() + idx);\n vector<int> right_inorder(inorder.begin() + idx + 1, inorder.end());\n\n root->left = build(preorder, left_inorder);\n root->right = build(preorder, right_inorder);\n\n return root;\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n deque<int> preq(preorder.begin(), preorder.end());\n return build(preq, inorder);\n }\n};", "memory": "45945" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 deque<int> preorderQueue(preorder.begin(), preorder.end());\n return build(preorderQueue, inorder); \n }\n\nprivate:\n TreeNode* build(deque<int>& preorder, vector<int>& inorder) {\n if (!inorder.empty()) {\n int val = preorder.front();\n preorder.pop_front();\n auto it = find(inorder.begin(), inorder.end(), val);\n int idx = it - inorder.begin();\n\n TreeNode* root = new TreeNode(val);\n vector<int> leftInorder(inorder.begin(), inorder.begin() + idx);\n vector<int> rightInorder(inorder.begin() + idx + 1, inorder.end());\n\n root->left = build(preorder, leftInorder);\n root->right = build(preorder, rightInorder);\n\n return root;\n }\n\n return nullptr;\n } \n};", "memory": "45945" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 deque <int> preorderdeque(preorder.begin(),preorder.end());\n return build(preorderdeque,inorder);\n }\n\nprivate:\n TreeNode* build(deque<int>& preorder, vector<int>& inorder) {\n if(!inorder.empty()){\n int val=preorder.front();\n preorder.pop_front();\n auto it = find(inorder.begin(),inorder.end(),val);\n int idx = it - inorder.begin();\n TreeNode* root=new TreeNode(val);\n vector<int> leftInorder(inorder.begin(),inorder.begin()+idx);\n vector<int> rightInorder(inorder.begin()+idx+1,inorder.end());\n\n root->left = build(preorder,leftInorder);\n root->right = build(preorder,rightInorder);\n return root;\n }\n return nullptr;\n }\n};", "memory": "48256" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 deque <int> preorderdeque(preorder.begin(),preorder.end());\n return build(preorderdeque,inorder);\n }\n\nprivate:\n TreeNode* build(deque<int>& preorder, vector<int>& inorder) {\n if(!inorder.empty()){\n int val=preorder.front();\n preorder.pop_front();\n auto it = find(inorder.begin(),inorder.end(),val);\n int idx = it - inorder.begin();\n TreeNode* root=new TreeNode(val);\n vector<int> leftInorder(inorder.begin(),inorder.begin()+idx);\n vector<int> rightInorder(inorder.begin()+idx+1,inorder.end());\n\n root->left = build(preorder,leftInorder);\n root->right = build(preorder,rightInorder);\n return root;\n }\n return nullptr;\n }\n};", "memory": "48256" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 deque<int> preorderQueue(preorder.begin(), preorder.end());\n return build(preorderQueue, inorder); \n }\n\nprivate:\n TreeNode* build(deque<int>& preorder, vector<int>& inorder) {\n if (!inorder.empty()) {\n int val = preorder.front();\n preorder.pop_front();\n auto it = find(inorder.begin(), inorder.end(), val);\n int idx = it - inorder.begin();\n\n TreeNode* root = new TreeNode(val);\n vector<int> leftInorder(inorder.begin(), inorder.begin() + idx);\n vector<int> rightInorder(inorder.begin() + idx + 1, inorder.end());\n\n root->left = build(preorder, leftInorder);\n root->right = build(preorder, rightInorder);\n\n return root;\n }\n\n return nullptr;\n } \n};", "memory": "50568" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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// class Solution {\n// public:\n// TreeNode*helper(vector<int>& preorder,int prestart,int preend,vector<int>& inorder,int instart,int inend,unordered_map<int,int>mpp){\n// if(prestart>preend || instart > inend) return nullptr;\n// TreeNode*root=new TreeNode(preorder[prestart]);\n// int mid=mpp[root->val];\n// int numl=mid-instart;\n// root->left=helper(preorder,prestart+1,numl+prestart,inorder,instart,mid-1,mpp);\n// root->right=helper(preorder,numl+prestart+1,preend,inorder,mid+1,inend,mpp);\n// return root;\n// }\n// TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n// int n=preorder.size();\n// if(n==0) return nullptr;\n// if(n==1) return new TreeNode(preorder[0]);\n// unordered_map<int,int>mpp;\n// for(int i=0;i<n;i++)mpp[inorder[i]]=i;\n// // TreeNode*root=new TreeNode(preorder[0]);\n// return helper(preorder,0,n-1,inorder,0,n-1,mpp);\n// }\n// };\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n deque<int> preorderQueue(preorder.begin(), preorder.end());\n return build(preorderQueue, inorder); \n }\n\nprivate:\n TreeNode* build(deque<int>& preorder, vector<int>& inorder) {\n if (!inorder.empty()) {\n int val = preorder.front();\n preorder.pop_front();\n auto it = find(inorder.begin(), inorder.end(), val);\n int idx = it - inorder.begin();\n\n TreeNode* root = new TreeNode(val);\n vector<int> leftInorder(inorder.begin(), inorder.begin() + idx);\n vector<int> rightInorder(inorder.begin() + idx + 1, inorder.end());\n\n root->left = build(preorder, leftInorder);\n root->right = build(preorder, rightInorder);\n\n return root;\n }\n\n return nullptr;\n } \n};", "memory": "50568" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 queue<TreeNode*> Tree;\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* root = new TreeNode();\n root = buildSubTree(preorder, inorder);\n return root;\n }\n\n TreeNode* buildSubTree(vector<int>& preorder, vector<int>& inorder) {\n TreeNode* node = new TreeNode();\n\n vector<int>::iterator it;\n it = find(inorder.begin(), inorder.end(), preorder[0]);\n if (it == inorder.end())\n return NULL;\n\n node->val = preorder[0];\n preorder.erase(preorder.begin());\n\n vector<int> leftInorder(inorder.begin(), it);\n vector<int> rightInorder(it + 1, inorder.end());\n\n node->left = buildSubTree(preorder, leftInorder);\n node->right = buildSubTree(preorder, rightInorder);\n\n return node;\n }\n};", "memory": "52879" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 {\nprivate:\n TreeNode* build(list<int>& preorder, vector<int>& inorder) {\n if(inorder.empty())\n return nullptr;\n \n int val = preorder.front();\n preorder.pop_front();\n\n TreeNode* node = new TreeNode(val);\n\n auto itr = find(inorder.begin(), inorder.end(), val);\n int idx = itr - inorder.begin();\n vector<int>left(inorder.begin(), inorder.begin()+idx);\n vector<int>right(inorder.begin()+idx+1, inorder.end());\n\n\n node->left = build(preorder, left);\n node->right = build(preorder, right);\n\n return node;\n }\n\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n list<int> pre(preorder.begin(), preorder.end());\n\n return build(pre, inorder);\n }\n};", "memory": "52879" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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\nTreeNode* makeTree(vector<vector<pair<int, bool>>> &adj, int u){\n // cout<<u<<endl;\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>& preorder, vector<int>& inorder) {\n int n = preorder.size();\n unordered_map<int, pair<int, bool>> parent;\n unordered_map<int, pair<int, int>> lim;\n unordered_map<int, int> ind;\n for(int i = 0; i < n; i++) ind[inorder[i]] = i;\n int index = ind[preorder[0]];\n lim[preorder[0]] = {0, n};\n for(int j = 0; j < index; j++) parent[inorder[j]] = {preorder[0], 0};\n for(int j = index + 1; j < n; j++) parent[inorder[j]] = {preorder[0], 1};\n // for(auto &&X: parent) cout<<X.first<<' '<<X.second.first<<' '<<X.second.second<<endl;\n for(int i = 1; i < n; i++){\n index = ind[preorder[i]];\n int parIndex = ind[parent[preorder[i]].first];\n int l = lim[parent[preorder[i]].first].first, u = lim[parent[preorder[i]].first].second;\n if(index > parIndex) l = parIndex + 1;\n else u = parIndex;\n lim[preorder[i]] = {l, u};\n for(int j = l; j < index; j++) parent[inorder[j]] = {preorder[i], 0};\n for(int j = index + 1; j < u; j++) parent[inorder[j]] = {preorder[i], 1};\n }\n vector<vector<pair<int, bool>>> adj(6010);\n for(int i = 1; i < n; i++) adj[parent[preorder[i]].first + 3000].push_back({preorder[i], parent[preorder[i]].second});\n TreeNode* root = makeTree(adj, preorder[0]);\n return root;\n }\n};", "memory": "55190" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 preIndex = 0;\n\nTreeNode* newTreeNode(int val) {\n\tTreeNode* t = new TreeNode;\n\tt->val = val;\n\tt->left = NULL;\n\tt->right = NULL;\n\n\treturn t;\n}\n\nint search(vector<int> inorder, int val) {\n\tfor (int i = 0; i < inorder.size(); i++) {\n\t\tif (inorder[i] == val)\n\t\t\treturn i;\n\t}\n\n\treturn -1;\n}\n\nTreeNode* subBuildTree(vector<int>& pre, vector<int>& in, int inStart, int inEnd) {\n\tif (inStart > inEnd) return NULL;\n\n\tTreeNode* tNode = newTreeNode(pre[preIndex++]);\n\n\tif (inStart == inEnd) return tNode;\n\tint inIndex = search(in, tNode->val);\n\n\t// Create left child\n\ttNode->left = subBuildTree(pre, in, inStart, inIndex - 1);\n\t// Creat right child\n\ttNode->right = subBuildTree(pre, in, inIndex + 1, inEnd);\n\n\treturn tNode;\n}\n\nTreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n\tpreIndex = 0;\n\treturn subBuildTree(preorder, inorder, 0, inorder.size() - 1);\n}\n};", "memory": "57501" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 pre=0;\n\n int find(vector<int> inorder, int val){\n for(int i=0; i<inorder.size();i++){\n if(inorder[i]==val)\n return i;\n }\n return -1;\n }\n\n TreeNode* util(vector<int>& preorder, vector<int>& inorder, int start, int end){\n if(start>end)\n return nullptr;\n TreeNode* T=new TreeNode(preorder[pre++]);\n if(start==end)\n return T;\n int idx = find(inorder, T->val);\n T->left = util(preorder, inorder, start, idx-1);\n T->right = util(preorder, inorder, idx+1, end);\n return T;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return util(preorder, inorder, 0, preorder.size()-1);\n }\n};", "memory": "59813" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 int search(vector<int> inorder,int start,int end,int curr){\n for(int i=start;i<=end;i++)\n {\n if(inorder[i]==curr)\n {\n return i;\n }\n }\n return -1;\n }\n TreeNode* helper(vector<int>&preorder,vector<int>&inorder,int start,int end,int& idx){\n if(start>end)\n {\n return nullptr;\n }\n int curr=preorder[idx];\n idx++;\n TreeNode* Node=new TreeNode(curr);\n if(start==end)\n {\n return Node;\n }\n int pos=search(inorder,start,end,curr);\n Node->left=helper(preorder,inorder,start,pos-1,idx);\n Node->right=helper(preorder,inorder,pos+1,end,idx);\n return Node;\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) \n {\n int idx=0;\n TreeNode* root=helper(preorder,inorder,0,inorder.size()-1,idx);\n return root;\n }\n};", "memory": "62124" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 search(vector<int> v, int s, int e, int val){\n for(int i=s; i<=e; i++){\n if(v[i] == val){\n return i;\n }\n }\n return -1;\n }\n\n TreeNode* tree(vector<int>& preorder, vector<int>& inorder, int s, int e, int& idx){\n if(s > e){\n return nullptr;\n }\n \n TreeNode* n = new TreeNode(preorder[idx]);\n idx++;\n if(s == e){\n return n;\n }\n\n int pos = search(inorder, s, e, n->val);\n \n n->left = tree(preorder, inorder, s, pos-1, idx);\n n->right = tree(preorder, inorder, pos+1, e, idx);\n\n return n;\n }\n \n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int idx = 0;\n return tree(preorder, inorder, 0, preorder.size()-1, idx);\n }\n};", "memory": "64435" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 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>&preorder,vector<int>&inorder,int start,int end,int& idx){\n if(start>end){\n return nullptr;\n }\n int curr=preorder[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->left=helper(preorder,inorder,start,pos-1,idx);\n Node->right=helper(preorder,inorder,pos+1,end,idx);\n return Node;\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int idx=0;\n TreeNode* root=helper(preorder,inorder,0,inorder.size()-1,idx);\n return root;\n }\n};", "memory": "66746" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 int inorderIndex(vector<int>inorder, int start, int end, int item) {\n for(int i = start; i <= end; i++) {\n if (inorder[i] == item) {\n return i;\n }\n }\n return -1;\n }\n TreeNode* buildTreeUtil(vector<int>& preorder, vector<int>& inorder, int* preIndex, int start, int end) {\n if(end < start) {\n return nullptr;\n }\n TreeNode* node = new TreeNode(preorder[*preIndex]);\n (*preIndex)++;\n if (start == end) {\n return node;\n }\n int index = inorderIndex(inorder, start, end, node->val);\n node->left = buildTreeUtil(preorder, inorder, preIndex, start, index - 1);\n node->right = buildTreeUtil(preorder, inorder, preIndex, index + 1, end);\n return node;\n }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int preIndex = 0;\n return buildTreeUtil(preorder, inorder, &preIndex, 0, preorder.size() - 1);\n }\n};", "memory": "69058" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if (preorder.empty() || inorder.empty()) return nullptr;\n\n queue<TreeNode*> q;\n unordered_map<int, vector<int>> mp;\n TreeNode *root = new TreeNode(preorder[0]);\n q.push(root);\n mp[root->val] = inorder;\n\n while (!q.empty()) {\n TreeNode* current = q.front();\n q.pop();\n \n int val = current->val;\n auto it = find(mp[val].begin(), mp[val].end(), val);\n\n vector<int> left(mp[val].begin(), it);\n vector<int> right(it + 1, mp[val].end());\n\n it = find(preorder.begin(), preorder.end(), val);\n bool isleft = false;\n if (!left.empty()) {\n it++;\n TreeNode *ln = new TreeNode(*it);\n current->left = ln;\n mp[*it] = left;\n q.push(ln);\n isleft = !isleft;\n }\n if (!right.empty()) {\n it = (isleft) ? (it + left.size()) : (it + 1);\n TreeNode *rn = new TreeNode(*it);\n current->right = rn;\n mp[*it] = right;\n q.push(rn);\n }\n }\n return root;\n }\n};\n", "memory": "71369" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if (preorder.empty() || inorder.empty()) return nullptr;\n\n queue<TreeNode*> q;\n unordered_map<int, vector<int>> mp;\n TreeNode *root = new TreeNode(preorder[0]);\n q.push(root);\n mp[root->val] = inorder;\n\n while (!q.empty()) {\n TreeNode* current = q.front();\n q.pop();\n \n int val = current->val;\n auto it = find(mp[val].begin(), mp[val].end(), val);\n\n vector<int> left(mp[val].begin(), it);\n vector<int> right(it + 1, mp[val].end());\n\n it = find(preorder.begin(), preorder.end(), val);\n bool isleft = false;\n if (!left.empty()) {\n it++;\n TreeNode *ln = new TreeNode(*it);\n current->left = ln;\n mp[*it] = left;\n q.push(ln);\n isleft = !isleft;\n }\n if (!right.empty()) {\n it = (isleft) ? (it + left.size()) : (it + 1);\n TreeNode *rn = new TreeNode(*it);\n current->right = rn;\n mp[*it] = right;\n q.push(rn);\n }\n }\n return root;\n }\n};\n", "memory": "71369" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 if (preorder.size() == 1) return new TreeNode(preorder[0]);\n TreeNode* root = new TreeNode(preorder[0]);\n auto rootInOrder = find(inorder.begin(), inorder.end(), preorder[0]);\n vector<int> leftInOrder(inorder.begin(), rootInOrder);\n vector<int> rightInOrder(rootInOrder + 1, inorder.end());\n vector<int> leftPreOrder(preorder.begin() + 1, preorder.begin() + 1 + leftInOrder.size());\n vector<int> rightPreOrder(preorder.begin() + 1 + leftInOrder.size(), preorder.end());\n\n root->left = buildTree(leftPreOrder, leftInOrder);\n root->right = buildTree(rightPreOrder, rightInOrder);\n return root;\n\n }\n};", "memory": "73680" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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() || inorder.empty()) return nullptr;\n TreeNode* root = new TreeNode();\n root->val = preorder[0];\n if (preorder.size() == 1) return root;\n auto prepoint = preorder.begin() + 1, inpoint = inorder.begin();\n while (*inpoint != root->val)\n {\n prepoint += 1;\n inpoint += 1;\n }\n vector<int> prevectorleft (preorder.begin()+1, prepoint);\n vector<int> prevectorright (prepoint, preorder.end());\n vector<int> invectorleft (inorder.begin(), inpoint);\n vector<int> invectorright (inpoint+1, inorder.end());\n root->left = buildTree(prevectorleft, invectorleft);\n root->right = buildTree(prevectorright, invectorright);\n\n return root;\n }\n};", "memory": "73680" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 find(int idx,vector<int> in, int start,int end){\n int i=start;\n while(i<=end){\n if(idx==in[i])return i;\n i++;\n }\n return 0;\n }\n // int find(int& idx,vector<int> pre,pair<vector<int>,int> in, int start,int end){\n \n // }\n TreeNode* build(vector<int>& pre,vector<int>& in, int instart,int inend,int& idx){\n if(instart>inend)return NULL;\n if(idx>pre.size()-1)return NULL;\n\n int i=find(pre[idx],in,instart,inend);\n TreeNode* node=new TreeNode(in[i]);\n (idx)++;\n node->left=build(pre,in,instart,i-1,idx);\n node->right=build(pre,in,i+1,inend,idx);\n return node;\n } \n\n\n \n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int n=inorder.size();\n int idx=0;\n TreeNode* ans=build(preorder,inorder,0,n-1,idx);\n return ans;\n }\n};", "memory": "75991" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 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>& preorder,int &index,int start,int end){\n if(index>=(inorder.size())|| start>end){\n return NULL;\n }\n int element=preorder[index++];\n TreeNode* root=new TreeNode(element);\n int position=findPosition(inorder,element);\n root->left= treeHelper(inorder,preorder,index,start,position - 1);\n root->right=treeHelper(inorder,preorder,index,position + 1,end);\n return root;\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int n=inorder.size()-1;\n int index=0;\n return treeHelper(inorder,preorder,index,0,n);\n \n }\n};", "memory": "75991" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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(const vector<int>& preorder, const vector<int>& inorder) {\n if (preorder.size() == 0) return nullptr;\n TreeNode* root = new TreeNode(preorder[0]);\n\n std::stack<int> stk;\n int nodes_on_left = 0;\n while (inorder[nodes_on_left] != preorder[0] && nodes_on_left < preorder.size()-1)\n {\n ++nodes_on_left;\n }\n // nodes on left are the leftmost nodes in preorder and inorder, right ones come after\n if (nodes_on_left < preorder.size())\n {\n if (nodes_on_left > 0)\n {\n // Attaches left\n // std::cout << \"Branching left from \" << root->val << \", nodes_on_left = \" << nodes_on_left << std::endl;\n // For preorder, +1 excludes the current node\n auto sub_preorder = std::vector<int>(preorder.begin()+1, preorder.begin()+1+nodes_on_left);\n // For inorder, current node is last on the left, so stop just before it\n auto sub_inorder = std::vector<int>(inorder.begin(), inorder.begin()+nodes_on_left);\n root->left = buildTree(sub_preorder, sub_inorder);\n\n }\n\n // Everything else is on the right\n // std::cout << \"Branching right from \" << root->val << \", nodes on right = \" << nodes_on_right << std::endl;\n auto sub_preorder = std::vector<int>(preorder.begin()+nodes_on_left+1, preorder.end());\n auto sub_inorder = std::vector<int>(inorder.begin()+nodes_on_left+1, inorder.end());\n root->right = buildTree(sub_preorder, sub_inorder);\n\n }\n \n \n return root;\n\n }\n};", "memory": "78303" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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(const vector<int>& preorder, const vector<int>& inorder) {\n if (preorder.size() == 0) return nullptr;\n TreeNode* root = new TreeNode(preorder[0]);\n\n std::stack<int> stk;\n int nodes_on_left = 0;\n while (inorder[nodes_on_left] != preorder[0] && nodes_on_left < preorder.size()-1)\n {\n ++nodes_on_left;\n }\n int nodes_on_right = preorder.size() - nodes_on_left - 1;\n if (nodes_on_left < preorder.size())\n {\n if (nodes_on_left > 0)\n {\n // Attaches left\n std::cout << \"Branching left from \" << root->val << \", nodes_on_left = \" << nodes_on_left << std::endl;\n auto sub_preorder = std::vector<int>(preorder.begin()+1, preorder.begin()+1+nodes_on_left);\n auto sub_inorder = std::vector<int>(inorder.begin(), inorder.begin()+nodes_on_left);\n root->left = buildTree(sub_preorder, sub_inorder);\n\n }\n\n // Attaches right\n std::cout << \"Branching right from \" << root->val << \", nodes on right = \" << nodes_on_right << std::endl;\n auto sub_preorder = std::vector<int>(preorder.begin()+nodes_on_left+1, preorder.end());\n auto sub_inorder = std::vector<int>(inorder.begin()+nodes_on_left+1, inorder.end());\n root->right = buildTree(sub_preorder, sub_inorder);\n\n \n\n }\n \n \n return root;\n\n }\n};", "memory": "80614" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 cout << preorder[0] << endl;\n TreeNode* root = new TreeNode(preorder[0]); // assume its the root of any substree\n int index = 0;\n for (int i = 0; i < inorder.size(); i++) {\n if (inorder[i] == preorder[0]) {\n index = i;\n break;\n }\n }\n // define left subtree\n vector<int> tmp1(preorder.begin() + 1, preorder.end()), tmp2(inorder.begin(), inorder.begin() + index);\n root->left = buildTree(tmp1, tmp2);\n\n // define right subtree\n vector<int> tmp3(preorder.begin() + index + 1, preorder.end()), \n tmp4(inorder.begin() + index + 1, inorder.end());\n root->right = buildTree(tmp3, tmp4);\n return root;\n }\n};", "memory": "80614" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 while(preorder.size() > 0 && search(inorder, preorder[0]) == -1){\n preorder = vector(preorder.begin() + 1, preorder.end());\n }\n if(preorder.size() < 1){\n return nullptr;\n }\n\n TreeNode* currentNode = new TreeNode(preorder[0]);\n int nodeIndex = search(inorder, preorder[0]);\n vector<int> left = vector(inorder.begin(), inorder.begin() + nodeIndex);\n vector<int> right = vector(inorder.begin() + nodeIndex + 1, inorder.end());\n vector<int> nextNode = vector(preorder.begin() + 1, preorder.end());\n\n if(left.size() > 0)\n currentNode->left = buildTree(nextNode, left);\n if(right.size() > 0)\n currentNode->right = buildTree(nextNode, right);\n return currentNode;\n }\n \n\n\n int search(vector<int>& inorder, int key){\n for(int i = 0; i < inorder.size(); i++){\n if(inorder[i] == key)\n return i;\n }\n return -1;\n }\n};", "memory": "82925" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 while(preorder.size() > 0 && search(inorder, preorder[0]) == -1){\n preorder = vector(preorder.begin() + 1, preorder.end());\n }\n if(preorder.size() < 1){\n return nullptr;\n }\n\n TreeNode* currentNode = new TreeNode(preorder[0]);\n int nodeIndex = search(inorder, preorder[0]);\n vector<int> left = vector(inorder.begin(), inorder.begin() + nodeIndex);\n vector<int> right = vector(inorder.begin() + nodeIndex + 1, inorder.end());\n vector<int> nextNode = vector(preorder.begin() + 1, preorder.end());\n //printVector(left);\n //printVector(right);\n \n //printVector(nextNode);\n //cout << binarySearch(right, 9);\n if(left.size() > 0)\n currentNode->left = buildTree(nextNode, left);\n if(right.size() > 0)\n currentNode->right = buildTree(nextNode, right);\n return currentNode;\n }\n void printVector(vector<int>& v){\n for(int num : v){\n cout << num << \" \";\n }\n cout << \"\\n\";\n }\n\n\n int search(vector<int>& inorder, int key){\n for(int i = 0; i < inorder.size(); i++){\n if(inorder[i] == key)\n return i;\n }\n return -1;\n }\n};", "memory": "85236" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 while(preorder.size() > 0 && search(inorder, preorder[0]) == -1){\n preorder = vector(preorder.begin() + 1, preorder.end());\n }\n if(preorder.size() < 1){\n return nullptr;\n }\n\n TreeNode* currentNode = new TreeNode(preorder[0]);\n int nodeIndex = search(inorder, preorder[0]);\n vector<int> left = vector(inorder.begin(), inorder.begin() + nodeIndex);\n vector<int> right = vector(inorder.begin() + nodeIndex + 1, inorder.end());\n vector<int> nextNode = vector(preorder.begin() + 1, preorder.end());\n\n if(left.size() > 0)\n currentNode->left = buildTree(nextNode, left);\n if(right.size() > 0)\n currentNode->right = buildTree(nextNode, right);\n return currentNode;\n }\n \n\n\n int search(vector<int>& inorder, int key){\n for(int i = 0; i < inorder.size(); i++){\n if(inorder[i] == key)\n return i;\n }\n return -1;\n }\n};", "memory": "85236" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 while(preorder.size() > 0 && search(inorder, preorder[0]) == -1){\n preorder = vector(preorder.begin() + 1, preorder.end());\n }\n if(preorder.size() < 1){\n return nullptr;\n }\n\n TreeNode* currentNode = new TreeNode(preorder[0]);\n int nodeIndex = search(inorder, preorder[0]);\n vector<int> left = vector(inorder.begin(), inorder.begin() + nodeIndex);\n vector<int> right = vector(inorder.begin() + nodeIndex + 1, inorder.end());\n vector<int> nextNode = vector(preorder.begin() + 1, preorder.end());\n //printVector(left);\n //printVector(right);\n \n //printVector(nextNode);\n //cout << binarySearch(right, 9);\n if(left.size() > 0)\n currentNode->left = buildTree(nextNode, left);\n if(right.size() > 0)\n currentNode->right = buildTree(nextNode, right);\n return currentNode;\n }\n void printVector(vector<int>& v){\n for(int num : v){\n cout << num << \" \";\n }\n cout << \"\\n\";\n }\n\n\n int search(vector<int>& inorder, int key){\n for(int i = 0; i < inorder.size(); i++){\n if(inorder[i] == key)\n return i;\n }\n return -1;\n }\n};", "memory": "87548" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 index = 0;\n return build(index, preorder, inorder);\n\n }\n\n TreeNode *build(int &preorderIndex, vector<int> &preorder, vector<int> &inorder)\n {\n if (inorder.size() == 0)\n {\n return nullptr;\n }\n\n vector<int> left_array = {};\n vector<int> right_array = {};\n \n int val = preorder[preorderIndex++];\n\n TreeNode *root = new TreeNode(val);\n splitTree(val, inorder, left_array, right_array);\n\n root->left = build(preorderIndex, preorder, left_array);\n root->right = build(preorderIndex, preorder, right_array);\n\n return root;\n }\n\n void splitTree(int root, const vector<int> &inorder, vector<int> &left, vector<int> &right)\n {\n bool root_found = false;\n for (int i=0; i < inorder.size(); i++)\n {\n if (inorder[i] == root)\n {\n root_found = true;\n continue;\n }\n\n if (root_found)\n {\n right.push_back(inorder[i]);\n }\n else\n {\n left.push_back(inorder[i]);\n }\n }\n }\n};", "memory": "89859" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 index = 0;\n return build(index, preorder, inorder);\n\n }\n\n TreeNode *build(int &preorderIndex, vector<int> &preorder, vector<int> &inorder)\n {\n if (inorder.size() == 0)\n {\n return nullptr;\n }\n\n vector<int> left_array = {};\n vector<int> right_array = {};\n \n int val = preorder[preorderIndex++];\n\n TreeNode *root = new TreeNode(val);\n splitTree(val, inorder, left_array, right_array);\n\n root->left = build(preorderIndex, preorder, left_array);\n root->right = build(preorderIndex, preorder, right_array);\n\n return root;\n }\n\n void splitTree(int root, const vector<int> &inorder, vector<int> &left, vector<int> &right)\n {\n bool root_found = false;\n for (int i=0; i < inorder.size(); i++)\n {\n if (inorder[i] == root)\n {\n root_found = true;\n continue;\n }\n\n if (root_found)\n {\n right.push_back(inorder[i]);\n }\n else\n {\n left.push_back(inorder[i]);\n }\n }\n }\n};", "memory": "92170" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 index = 0;\n return build(index, preorder, inorder);\n\n }\n\n TreeNode *build(int &preorderIndex, vector<int> &preorder, vector<int> &inorder)\n {\n if (inorder.size() == 0)\n {\n return nullptr;\n }\n\n vector<int> left_array = {};\n vector<int> right_array = {};\n \n int val = preorder[preorderIndex++];\n\n TreeNode *root = new TreeNode(val);\n splitTree(val, inorder, left_array, right_array);\n\n root->left = build(preorderIndex, preorder, left_array);\n root->right = build(preorderIndex, preorder, right_array);\n\n return root;\n }\n\n void splitTree(int root, const vector<int> &inorder, vector<int> &left, vector<int> &right)\n {\n bool root_found = false;\n for (int i=0; i < inorder.size(); i++)\n {\n if (inorder[i] == root)\n {\n root_found = true;\n continue;\n }\n\n if (root_found)\n {\n right.push_back(inorder[i]);\n }\n else\n {\n left.push_back(inorder[i]);\n }\n }\n }\n};", "memory": "92170" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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{\n public:\n int ls(int key, vector<int> &inorder)\n {\n int n = inorder.size();\n for (int i = 0; i < n; i++)\n {\n if (key == inorder[i]) return i;\n }\n return -1;\n }\n TreeNode* buildTree(vector<int> &preorder, vector<int> &inorder)\n {\n if (preorder.size() <= 0 || inorder.size() <= 0) return nullptr;\n TreeNode *root = new TreeNode(preorder[0]);\n preorder.erase(preorder.begin(), preorder.begin() + 1);\n if (inorder.size() == 1) return root;\n \n int mid = ls(root->val, inorder);\n\n vector<int> left;\n for(int i = 0;i<mid;i++) left.push_back(inorder[i]);\n root->left = buildTree(preorder, left);\n\n vector<int> right;\n for(int i = mid+1;i<inorder.size();i++) right.push_back(inorder[i]);\n root->right = buildTree(preorder, right);\n \n return root;\n }\n};", "memory": "94481" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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(inorder.size() == 0) return NULL;\n TreeNode * root = new TreeNode(preorder[0]);\n preorder.erase(preorder.begin() + 0);\n vector<int> left;\n int i = 0;\n for(; i < inorder.size(); i++) {\n if(inorder[i] == root->val) break;\n left.push_back(inorder[i]);\n }\n i++;\n vector<int> right;\n for(; i < inorder.size(); i++) {\n right.push_back(inorder[i]);\n }\n root->left = buildTree(preorder, left);\n root->right = buildTree(preorder, right);\n return root;\n }\n};", "memory": "94481" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 {\nprivate:\n int findPos(vector<int> v, int target) {\n int l = 0, r = v.size()-1;\n\n for(auto i=0; i<v.size(); i++) {\n if(v[i] == target) return i;\n }\n\n return -1;\n }\n\n TreeNode* build(vector<int>& pre, vector<int>& in, TreeNode* node) {\n \n node->val = pre[0];\n if(pre.size() == 1) return node;\n\n int pos = findPos(in, pre[0]);\n\n if(pos != 0) {\n TreeNode* left = new TreeNode();\n vector<int> pre_l = {pre.begin()+1, pre.begin()+pos+1};\n vector<int> pre_r = {in.begin(), in.begin()+pos};\n left = build(pre_l, pre_r, left);\n node->left = left;\n }\n\n if(pos != in.size()-1) {\n TreeNode* right = new TreeNode(); \n vector<int> in_l = {pre.begin()+pos+1, pre.end()};\n vector<int> in_r = {in.begin()+pos+1, in.end()};\n right = build(in_l, in_r, right);\n node->right = right;\n }\n \n return node;\n }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n\n if(!preorder.size()) {\n return NULL;\n }\n \n TreeNode* root = new TreeNode();\n build(preorder, inorder, root);\n\n return root;\n }\n};", "memory": "96793" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 if(preorder.size() == 0){\n return nullptr;\n }\n else if(preorder.size() == 1){\n return new TreeNode(preorder[0]);\n }\n else{\n // Find root\n int root = preorder[0];\n\n // Find left\n int root_index = index(inorder, root);\n vector<int> left_inorder = vector<int>(inorder.begin(), inorder.begin()+root_index);\n vector<int> left_preorder = vector<int>(preorder.begin() + 1, preorder.begin() + 1 + root_index);\n\n // Find right\n vector<int> right_inorder = vector<int>(inorder.begin() + root_index + 1, inorder.begin() + inorder.size());\n vector<int> right_preorder = vector<int>(preorder.begin() + root_index + 1, preorder.end());\n\n // Make left and right subtrees\n TreeNode* left_tree = buildTree(left_preorder, left_inorder);\n TreeNode* right_tree = buildTree(right_preorder, right_inorder);\n\n return new TreeNode(root, left_tree, right_tree);\n }\n \n }\n\n int index(vector<int> sequence, int value){\n auto it = find(sequence.begin(), sequence.end(), value);\n return distance(sequence.begin(), it);\n }\n\n bool contained(vector<int> sequence, int value){\n auto it = find(sequence.begin(), sequence.end(), value);\n return it != sequence.end();\n }\n};", "memory": "96793" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 TreeNode* root = new TreeNode(preorder[0]);\n if(preorder.size()==1) return root;\n vector<int> leftInOrder;\n for(int i=0;i<inorder.size();i++){\n if(inorder[i]==preorder[0]){\n //this is the root index\n //left tree -> [0, i-1]\n //right tree -> [i+1, inorder.size()-1]\n vector<int> rightInOrder(inorder.begin()+i+1, inorder.end());\n vector<int> leftPreOrder(preorder.begin()+1, preorder.begin()+1+leftInOrder.size());\n vector<int> rightPreOrder(preorder.begin()+1+leftInOrder.size(), preorder.end());\n root->left = buildTree(leftPreOrder, leftInOrder);\n root->right = buildTree(rightPreOrder, rightInOrder);\n break;\n }else{\n leftInOrder.push_back(inorder[i]);\n }\n }\n return root;\n }\n};\n\n//[3 9 20 15 7] [9 3 15 20 7]\n//[9] [9] [20 15 7] [15 20 7]", "memory": "99104" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 pair<vector<int>, vector<int>> findNeighbours(const vector<int>& v, const int&purpose)\n {\n vector<int> left;\n vector<int> right;\n\n bool wasPurpose = false;\n\n for (auto&i:v)\n {\n if (i == purpose)\n {\n wasPurpose = true;\n continue;\n }\n\n if (!wasPurpose)\n {\n left.emplace_back(i);\n }\n else\n {\n right.emplace_back(i);\n }\n }\n return make_pair(left, right);\n }\n\n vector<int> preorder;\n int counter = 0;\n\n TreeNode* builder(vector<int>& inorder) {\n if (counter >= preorder.size()) { return nullptr; };\n if (inorder.empty()) { return nullptr; }\n\n int val = preorder[counter];\n counter++;\n\n auto neighbours = findNeighbours(inorder, val); \n\n TreeNode* node = new TreeNode(val);\n node->left = builder(neighbours.first);\n node->right = builder(neighbours.second);\n\n return node;\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n this->preorder = preorder;\n return builder(inorder);\n }\n};", "memory": "101415" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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* find(vector<int> inorder,vector<int>& preorder)\n {\n if(!inorder.size())\n return NULL;\n \n int flag=0;\n vector<int> inorder_left,inorder_right;\n TreeNode *root=new TreeNode(preorder[0]);\n\n for(int i=0;i<inorder.size();i++)\n {\n if(inorder[i]==preorder[0])\n {\n flag=1;\n continue;\n }\n\n else if(!flag)\n inorder_left.emplace_back(inorder[i]);\n\n else\n inorder_right.emplace_back(inorder[i]);\n }\n\n preorder.erase(preorder.begin());\n root->left=find(inorder_left,preorder);\n root->right=find(inorder_right,preorder);\n\n return root;\n }\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n return (find(inorder,preorder));\n }\n};", "memory": "103726" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 void solve(TreeNode* root, vector<int>& preorder, int& preorder_index,\n vector<int> inorder, int inorder_index) {\n if (preorder_index == preorder.size()) {\n return;\n }\n vector<int> left, right;\n bool left_found = false, right_found = false;\n int index;\n for (int i = 0; i < inorder_index; i++) {\n left.push_back(inorder[i]);\n if (inorder[i] == preorder[preorder_index]) {\n left_found = true;\n index = i;\n }\n }\n\n if (left_found) {\n TreeNode* temp = new TreeNode(preorder[preorder_index]);\n root->left = temp;\n preorder_index++;\n solve(temp, preorder, preorder_index, left, index);\n }\n\n for (int i = inorder_index + 1; i < inorder.size(); i++) {\n right.push_back(inorder[i]);\n if (inorder[i] == preorder[preorder_index]) {\n right_found = true;\n index = i - inorder_index - 1;\n }\n }\n\n if (right_found) {\n TreeNode* temp = new TreeNode(preorder[preorder_index]);\n root->right = temp;\n preorder_index++;\n solve(temp, preorder, preorder_index, right, index);\n }\n\n if (!left_found && !right_found) {\n return;\n }\n }\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n\n TreeNode* root = new TreeNode(preorder[0]);\n int inorder_index, preorder_index = 1;\n for (int i = 0; i < inorder.size(); i++) {\n if (inorder[i] == preorder[0]) {\n inorder_index = i;\n break;\n }\n }\n\n solve(root, preorder, preorder_index, inorder, inorder_index);\n\n return root;\n }\n};", "memory": "106038" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 {\nprivate:\n bool splitInorder(vector<int>& inorder, vector<int>& left, vector<int>& right, int val) {\n if (inorder.size() == 0) return false;\n bool found = false;\n for (auto n : inorder) {\n if (n == val) {\n found = true;\n continue;\n }\n if (!found) left.push_back(n);\n else right.push_back(n);\n }\n return true;\n }\n\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n if (preorder.size() == 0) return nullptr;\n TreeNode* node = new TreeNode();\n node->val = preorder[0];\n vector<int> left, right;\n if (!splitInorder(inorder, left, right, preorder[0])) return node;\n preorder.erase(preorder.begin());\n vector<int> preorder_l;\n preorder_l.insert(preorder_l.begin(), preorder.begin(),preorder.begin() + left.size());\n node->left = buildTree(preorder_l, left);\n preorder.erase(preorder.begin(), preorder.begin() + left.size());\n node->right = buildTree(preorder, right);\n return node;\n }\n};", "memory": "108349" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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* dfsConstruct(vector<int>& preorder, vector<int> currInorder, int& index){\n if(index >= preorder.size()){\n return nullptr;\n }\n TreeNode* currNode = new TreeNode(preorder[index]);\n vector<int> nums;\n int start = 0;\n for(int i = 0; i < currInorder.size(); i++){\n if(currInorder[i] == preorder[index]){\n start = i;\n break;\n }\n nums.push_back(currInorder[i]);\n }\n\n if(nums.size() == 0){\n currNode->left = nullptr;\n } else {\n \n currNode->left = dfsConstruct(preorder, nums, ++index);\n }\n\n nums.clear();\n for(int i = start + 1; i < currInorder.size(); i++){\n nums.push_back(currInorder[i]);\n }\n\n if(nums.size() == 0){\n currNode->right = nullptr;\n } else {\n currNode->right = dfsConstruct(preorder, nums, ++index);\n }\n\n return currNode;\n }\npublic:\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int index = 0;\n return dfsConstruct(preorder, inorder, index);\n }\n};", "memory": "110660" }
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> 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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= preorder.length &lt;= 3000</code></li> <li><code>inorder.length == preorder.length</code></li> <li><code>-3000 &lt;= preorder[i], inorder[i] &lt;= 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 TreeNode* rec(vector<int> preorder, unordered_map<int,int>& inorderMap) {\n int n=preorder.size();\n if(n==0)return NULL;\n TreeNode* head=new TreeNode();\n\n head->val=preorder[0];\n\n vector<int> f,l;\n for(int i=1;i<n;i++){\n if( inorderMap[preorder[i]] > inorderMap[preorder[0]]){\n l.push_back(preorder[i]);\n } \n else {\n f.push_back(preorder[i]);\n }\n }\n head->left=rec(f,inorderMap);\n head->right=rec(l,inorderMap);\n\n return head;\n\n\n\n }\n\n\n TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {\n int n=inorder.size();\n unordered_map<int,int> inOrderMap;\n for(int i=0;i<n;i++)inOrderMap[inorder[i]]=i;\n return rec(preorder, inOrderMap);\n }\n\n\n\n};", "memory": "112971" }