id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
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(const vector<int>& inorder, const vector<int>& postorder, unordered_map<int,int>& inorder_rev, int inorder_left, int inorder_right) {\n int root = postorder[postorder.size()-1];\n int root_pos_inorder = inorder_rev[root];\n vector<int> postorder_left, postorder_right;\n for (int i = 0; i < postorder.size()-1; ++i) {\n int val = postorder[i];\n int pos_inorder = inorder_rev[val];\n if (pos_inorder < inorder_left || pos_inorder > inorder_right) {\n continue;\n }\n if (pos_inorder < root_pos_inorder) {\n postorder_left.push_back(val);\n }\n else {\n postorder_right.push_back(val);\n }\n }\n TreeNode* node = new TreeNode(root);\n if (!postorder_left.empty()) {\n node->left = build(inorder, postorder_left, inorder_rev, inorder_left, root_pos_inorder-1);\n }\n if (!postorder_right.empty()) {\n node->right = build(inorder, postorder_right, inorder_rev, root_pos_inorder+1, inorder_right);\n }\n return node;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n unordered_map<int,int> inorder_rev;\n for (int i = 0; i < inorder.size(); ++i) {\n inorder_rev[inorder[i]] = i;\n }\n return build(inorder, postorder, inorder_rev, 0, inorder.size() - 1);\n }\n};", "memory": "85259" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
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 findvector(vector<int> vec,int target)\n {\n for(int i=0;i<vec.size();i++)\n if(vec[i]==target)\n return i;\n return -1;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if(inorder.size()==0)\n return nullptr;\n else\n {\n int num=postorder[postorder.size()-1];\n int k=findvector(inorder,num);\n vector<int> leftinorder(inorder.begin(),inorder.begin()+k);\n vector<int> leftpostorder(postorder.begin(),postorder.begin()+k);\n vector<int> rightinorder(inorder.begin()+k+1,inorder.end());\n vector<int> rightpostorder(postorder.begin()+k,postorder.end()-1);\n TreeNode* root=new TreeNode;\n root->val=num;\n root->left=buildTree(leftinorder,leftpostorder);\n root->right=buildTree(rightinorder,rightpostorder);\n return root;\n }\n }\n};", "memory": "86500" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/*\n * start at end of postorder o/w same?\n *\n */\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n TreeNode* root = build(postorder, inorder);\n\n return root;\n }\n\n TreeNode* build(vector<int>& post_o, vector<int>& in_o) {\n if ((0 == in_o.size()) || (0 == post_o.size())) {\n return NULL;\n }\n TreeNode* node = new TreeNode();\n if (1 == post_o.size()) {\n node->val = post_o[0];\n return node;\n }\n\n int curr = post_o[post_o.size()-1];\n ptrdiff_t in_idx = distance(in_o.begin(), find(in_o.begin(), in_o.end(), curr));\n vector<int> in_left = {in_o.begin(), in_o.begin()+in_idx};\n int left_len = in_left.size();\n vector<int> in_right = {in_o.begin()+(in_idx+1), in_o.end()};\n int right_len = in_right.size();\n\n node->val = curr;\n vector<int> left_in = {in_o.begin(), in_o.begin()+in_idx};\n vector<int> left_post = {post_o.begin(), post_o.begin()+left_len};\n node->left = build(left_post, left_in);\n vector<int> right_in = {in_o.begin()+(in_idx+1), in_o.end()};\n vector<int> right_post = {post_o.begin()+left_len, post_o.begin()+(left_len+right_len)};\n node->right = build(right_post, right_in);\n\n return node;\n }\n};", "memory": "87741" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n unordered_map<int, int> indices;\n\n for (int i = 0; i < inorder.size(); ++i) {\n indices[inorder[i]] = i;\n }\n\n reverse(postorder.begin(), postorder.end());\n\n TreeNode *root = new TreeNode(postorder[0]);\n TreeNode *node = root;\n\n stack<TreeNode *> parents;\n stack<int> parent_indices;\n parents.push(node);\n parent_indices.push(indices[node->val]);\n\n for (int i = 1; i < postorder.size(); ++i) {\n int next_index = indices[postorder[i]];\n int curr_index = indices[node->val];\n\n if (next_index > curr_index) {\n TreeNode *right = new TreeNode(postorder[i]);\n node->right = right;\n parents.push(node);\n parent_indices.push(curr_index);\n node = node->right;\n } else {\n while (true) {\n node = parents.top();\n int parent_index = parent_indices.top();\n\n if (parent_index < next_index) {\n node = node->right;\n break;\n }\n\n parents.pop();\n parent_indices.pop();\n\n if (node == root)\n break;\n }\n\n while (node->left) {\n parents.push(node);\n parent_indices.push(indices[node->val]);\n node = node->left;\n }\n\n node->left = new TreeNode(postorder[i]);\n parents.push(node);\n parent_indices.push(indices[node->val]);\n node = node->left;\n }\n }\n\n return root;\n }\n};", "memory": "88983" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n unordered_map<int, int> indices;\n\n for (int i = 0; i < inorder.size(); ++i) {\n indices[inorder[i]] = i;\n }\n\n TreeNode *root = new TreeNode(postorder[postorder.size() - 1]);\n TreeNode *node = root;\n\n stack<TreeNode *> parents;\n stack<int> parent_indices;\n parents.push(node);\n parent_indices.push(indices[node->val]);\n\n for (int i = postorder.size() - 2; i >= 0; --i) {\n int next_index = indices[postorder[i]];\n int curr_index = indices[node->val];\n\n if (next_index > curr_index) {\n TreeNode *right = new TreeNode(postorder[i]);\n node->right = right;\n parents.push(node);\n parent_indices.push(curr_index);\n node = node->right;\n } else {\n while (true) {\n node = parents.top();\n int parent_index = parent_indices.top();\n\n if (parent_index < next_index) {\n node = node->right;\n break;\n }\n\n parents.pop();\n parent_indices.pop();\n\n if (node == root)\n break;\n }\n\n while (node->left) {\n parents.push(node);\n parent_indices.push(indices[node->val]);\n node = node->left;\n }\n\n node->left = new TreeNode(postorder[i]);\n parents.push(node);\n parent_indices.push(indices[node->val]);\n node = node->left;\n }\n }\n\n return root;\n }\n};", "memory": "90224" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n=inorder.size();\n if(n==0) return NULL;\n TreeNode* a=new TreeNode(postorder[n-1]);\n int i;\n for(i=0; i<n; i++){\n if(inorder[i]==postorder[n-1]) break;\n }\n vector<int>b(inorder.begin(),inorder.begin()+i);\n vector<int>c(inorder.begin()+i+1,inorder.end());\n vector<int>d,e;\n int j;\n for(i=0; i<b.size(); i++)\n d.push_back(postorder[i]);\n for(j=i; j<i+c.size(); j++)\n e.push_back(postorder[j]);\n a->left=buildTree(b,d);\n a->right=buildTree(c,e);\n return a;\n }\n};", "memory": "91465" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void build(TreeNode* &root,vector<int> in,vector<int>& post){\n int n;\n if(post.size()>=1){\n n=post[post.size()-1];\n }\n if(in.size()<=0){\n root=nullptr;\n return;\n }\n else{\n root=new TreeNode(n);\n }\n vector<int> v_l,v_r; bool flag=false;\n for(int i=0; i<in.size(); i++){\n if(in[i]==n){\n flag=true;\n }\n else if(in[i]!=n&&!flag){\n v_l.push_back(in[i]);\n }\n else{\n v_r.push_back(in[i]);\n }\n }\n post.pop_back();\n build(root->right,v_r,post);\n build(root->left,v_l,post);\n\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n TreeNode* root=nullptr; \n build(root,inorder,postorder);\n return root;\n }\n};", "memory": "92706" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void build(TreeNode* &root,vector<int> in,vector<int>& post){\n int n;\n if(post.size()>=1){\n n=post[post.size()-1];\n }\n if(in.size()<=0){\n root=nullptr;\n return;\n }\n else{\n root=new TreeNode(n);\n }\n vector<int> v_l,v_r; \n for(int i=0 ,j=in.size()-1; i!=j;){\n if(in[i]!=n){\n v_l.push_back(in[i]);\n i++;\n }\n if(in[j]!=n){\n v_r.push_back(in[j]);\n j--;\n }\n \n }\n reverse(v_r.begin(),v_r.end());\n post.pop_back();\n build(root->right,v_r,post);\n build(root->left,v_l,post);\n\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n TreeNode* root=nullptr; \n build(root,inorder,postorder);\n return root;\n }\n};", "memory": "93948" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if (inorder.size() == 0){\n return nullptr;\n }\n if (inorder.size() == 1){\n return new TreeNode(inorder[0]);\n }\n int n = postorder.size();\n TreeNode* root = new TreeNode(postorder[n-1]);\n int i = 0;\n while (inorder[i] != root->val){\n i++;\n }\n vector<int> in_left(inorder.begin(), inorder.begin()+i);\n // vector<int> in_right;\n // vector<int> post_left;\n // vector<int> post_right;\n\n // vector<int> in_left;\n vector<int> in_right;\n vector<int> post_left;\n vector<int> post_right;\n // for (int j = 0; j < i; j++){\n // in_left.push_back(inorder[j]);\n // }\n for (int j = i+1; j < n; j++){\n in_right.push_back(inorder[j]);\n }\n int l_size = in_left.size();\n for (int j = 0; j < l_size; j++){\n post_left.push_back(postorder[j]);\n }\n for (int j = l_size; j < n-1; j++){\n post_right.push_back(postorder[j]);\n }\n root->left = buildTree(in_left, post_left);\n root->right = buildTree(in_right, post_right);\n return root;\n \n }\n};\n\n\n// class Solution:\n// def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:\n// n = len(postorder)\n// if n == 1:\n// return TreeNode(postorder[0])\n// def r(postord, inord):\n// if not postord: return None\n// if len(postord) == 1: return TreeNode(postord[0])\n// v = postord[-1]\n// root = TreeNode(v)\n// i = inord.index(v)\n// postl = postord[:i]\n// postr = postord[i:-1]\n// #prel = preord[1:i+1]\n// #prer = preord[i+1:]\n// inl = inord[:i]\n// inr = inord[i+1:]\n// root.left = r(postl, inl)\n// root.right = r(postr, inr)\n// return root\n// return r(postorder, inorder) ", "memory": "95189" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n int search(vector<int>&inorder,int start,int end,int key){\n for(int i=start;i<=end;i++){\n if(inorder[i]==key)\n return i;\n }\n return -1;\n}\nTreeNode*helper(vector<int>&inorder,vector<int>postorder,int start,int end ,int &idx){\n if(start>end) return nullptr;\n int val=postorder[idx];\n idx--;\n TreeNode*curr=new TreeNode(val);\n if(start==end) return curr;\n int pos=search(inorder,start,end,val);\n \n curr->right=helper(inorder,postorder,pos+1,end,idx);\n curr->left=helper(inorder,postorder,start,pos-1,idx);\n return curr;\n}\n TreeNode* buildTree(vector<int>&inorder, vector<int>& postorder) {\n int n=postorder.size() ;\n int idx=n-1;\n TreeNode*root=helper(inorder,postorder,0,n-1,idx);\n return root;\n }\n};", "memory": "96430" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n private:\n unordered_map<int, int> indexMap;\n\n TreeNode* buildTreeCheck(vector<int> &inorder, vector<int> postorder, int left, int right, int &index){\n if(left>right)return nullptr;\n\n TreeNode *curr = new TreeNode(postorder[index--]);\n if(left == right)return curr;\n \n int inorderIndex = indexMap[curr->val];\n curr->right = buildTreeCheck(inorder, postorder, inorderIndex + 1, right, index);\n curr->left = buildTreeCheck(inorder, postorder, left, inorderIndex - 1, index);\n\n return curr;\n }\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n for(int i = 0;i<postorder.size(); i++){\n indexMap[inorder[i]] = i;\n }\n\n int index = postorder.size()-1;\n\n return buildTreeCheck(inorder, postorder, 0, index, index);\n\n }\n};", "memory": "97671" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
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 postInd;\n unordered_map<int, int> inorderMap;\n TreeNode* util(vector<int> postorder, int inInd, int n){\n if(inInd > n) return NULL;\n\n TreeNode* root = new TreeNode(postorder[postInd--]);\n if(inInd == n) return root;\n\n int inorderRootInd = inorderMap[root->val];\n cout<<root->val<<\" \"<<inorderRootInd<<endl;\n\n root->right = util(postorder, inorderRootInd + 1, n);\n root->left = util(postorder, inInd, inorderRootInd - 1);\n \n\n return root;\n\n\n }\n\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n = inorder.size();\n\n postInd = n - 1;\n\n for(int i = 0; i < n; i++) inorderMap[inorder[i]] = i;\n\n TreeNode* root = util(postorder, 0, n - 1);\n\n return root;\n }\n};", "memory": "98913" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
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 postInd;\n unordered_map<int, int> inorderMap;\n TreeNode* util(vector<int> postorder, int inInd, int n){\n if(inInd > n) return NULL;\n\n TreeNode* root = new TreeNode(postorder[postInd--]);\n if(inInd == n) return root;\n\n int inorderRootInd = inorderMap[root->val];\n\n root->right = util(postorder, inorderRootInd + 1, n);\n root->left = util(postorder, inInd, inorderRootInd - 1);\n \n\n return root;\n\n\n }\n\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n = inorder.size();\n\n postInd = n - 1;\n\n for(int i = 0; i < n; i++) inorderMap[inorder[i]] = i;\n\n TreeNode* root = util(postorder, 0, n - 1);\n\n return root;\n }\n};", "memory": "100154" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n private:\n unordered_map<int, int> indexMap;\n\n TreeNode* buildTreeCheck(vector<int> &inorder, vector<int> postorder, int left, int right, int &index){\n if(left>right)return nullptr;\n\n TreeNode *curr = new TreeNode(postorder[index--]);\n if(left == right)return curr;\n \n int inorderIndex = indexMap[curr->val];\n curr->right = buildTreeCheck(inorder, postorder, inorderIndex + 1, right, index);\n curr->left = buildTreeCheck(inorder, postorder, left, inorderIndex - 1, index);\n\n return curr;\n }\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n for(int i = 0;i<postorder.size(); i++){\n indexMap[inorder[i]] = i;\n }\n\n int index = postorder.size()-1;\n\n return buildTreeCheck(inorder, postorder, 0, index, index);\n\n }\n};", "memory": "101395" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
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* help(vector<int> postorder, int start, int end, unordered_map<int,int> &inorderIndex, int& i, vector<int> &inorder){\n if( end < start || i < 0 )\n return NULL;\n \n TreeNode* node = new TreeNode(postorder[i]);\n int index = inorderIndex[postorder[i]];\n inorderIndex.erase(postorder[i]);\n i--;\n\n if( end == start ){\n TreeNode *node = new TreeNode(inorder[start]);\n return node;\n }\n \n node->right = help(postorder, index+1, end, inorderIndex, i, inorder);\n node->left = help(postorder, start, index-1, inorderIndex, i, inorder);\n\n return node;\n \n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n unordered_map<int,int> inorderIndex;\n\n for( int i = 0; i < inorder.size(); i++){\n inorderIndex[inorder[i]] = i;\n }\n \n int n = postorder.size()-1;\n return help(postorder, 0, n, inorderIndex, n, inorder);\n\n }\n};", "memory": "102636" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n return traversal(inorder, postorder);\n };\n\n TreeNode* traversal(vector<int> inorder, vector<int> postorder) {\n // empty postorder vector (end condition)\n if (postorder.size()==0) {\n return nullptr;\n }\n\n // Last entry of postorder is the current \"mid\"\n int midValue = postorder[postorder.size()-1];\n TreeNode* mid = new TreeNode(midValue);\n\n if (postorder.size()==1) {\n return mid;\n }\n\n // Find midValue in inorder vector\n // inorder vector = [left tree] + mid + [right tree]\n int midIndex = 0;\n while (midIndex < inorder.size()) {\n if (inorder[midIndex] == midValue) {\n break;\n }\n midIndex++;\n }\n\n // Get left tree's vectors and right tree's vectors\n vector<int> left_inorder(inorder.begin(), inorder.begin() + midIndex);\n vector<int> right_inorder(inorder.begin() + midIndex + 1, inorder.end() );\n vector<int> left_postorder(postorder.begin(), postorder.begin() + left_inorder.size());\n vector<int> right_postorder(postorder.begin() + left_inorder.size(), postorder.end()-1);\n\n // Get left and right subtrees\n mid->left = traversal(left_inorder, left_postorder);\n mid->right = traversal(right_inorder, right_postorder);\n return mid;\n };\n};", "memory": "103878" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n return traversal(inorder, postorder);\n };\n\n TreeNode* traversal(vector<int> inorder, vector<int> postorder) {\n // empty postorder vector (end condition)\n if (postorder.size()==0) {\n return nullptr;\n }\n\n // Last entry of postorder is the current \"mid\"\n int midValue = postorder[postorder.size()-1];\n TreeNode* mid = new TreeNode(midValue);\n\n if (postorder.size()==1) {\n return mid;\n }\n\n // Find midValue in inorder vector\n // inorder vector = [left tree] + mid + [right tree]\n int midIndex = 0;\n while (midIndex < inorder.size()) {\n if (inorder[midIndex] == midValue) {\n break;\n }\n midIndex++;\n }\n\n // Get left tree's vectors and right tree's vectors\n vector<int> left_inorder(inorder.begin(), inorder.begin() + midIndex);\n vector<int> right_inorder(inorder.begin() + midIndex + 1, inorder.end() );\n vector<int> left_postorder(postorder.begin(), postorder.begin() + left_inorder.size());\n vector<int> right_postorder(postorder.begin() + left_inorder.size(), postorder.end()-1);\n\n // Get left and right subtrees\n mid->left = traversal(left_inorder, left_postorder);\n mid->right = traversal(right_inorder, right_postorder);\n return mid;\n };\n};", "memory": "105119" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n return traversal(inorder, postorder);\n };\n\n TreeNode* traversal(vector<int> inorder, vector<int> postorder) {\n // empty postorder vector (end condition)\n if (postorder.size()==0) {\n return nullptr;\n }\n\n // Last entry of postorder is the current \"mid\"\n int midValue = postorder[postorder.size()-1];\n TreeNode* mid = new TreeNode(midValue);\n\n if (postorder.size()==1) {\n return mid;\n }\n\n // Find midValue in inorder vector\n // inorder vector = [left tree] + mid + [right tree]\n int midIndex = 0;\n while (midIndex < inorder.size()) {\n if (inorder[midIndex] == midValue) {\n break;\n }\n midIndex++;\n }\n\n // Get left tree's vectors and right tree's vectors\n vector<int> left_inorder(inorder.begin(), inorder.begin() + midIndex);\n vector<int> right_inorder(inorder.begin() + midIndex + 1, inorder.end() );\n vector<int> left_postorder(postorder.begin(), postorder.begin() + left_inorder.size());\n vector<int> right_postorder(postorder.begin() + left_inorder.size(), postorder.end()-1);\n\n // Get left and right subtrees\n mid->left = traversal(left_inorder, left_postorder);\n mid->right = traversal(right_inorder, right_postorder);\n return mid;\n };\n};", "memory": "105119" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n return traversal(inorder, postorder);\n };\n\n TreeNode* traversal(vector<int> inorder, vector<int> postorder) {\n // empty postorder vector (end condition)\n if (postorder.size()==0) {\n return nullptr;\n }\n\n // Last entry of postorder is the current \"mid\"\n int midValue = postorder[postorder.size()-1];\n TreeNode* mid = new TreeNode(midValue);\n\n if (postorder.size()==1) {\n return mid;\n }\n\n // Find midValue in inorder vector\n // inorder vector = [left tree] + mid + [right tree]\n int midIndex = 0;\n while (midIndex < inorder.size()) {\n if (inorder[midIndex] == midValue) {\n break;\n }\n midIndex++;\n }\n\n // Get left tree's vectors and right tree's vectors\n vector<int> left_inorder(inorder.begin(), inorder.begin() + midIndex);\n vector<int> right_inorder(inorder.begin() + midIndex + 1, inorder.end() );\n vector<int> left_postorder(postorder.begin(), postorder.begin() + left_inorder.size());\n vector<int> right_postorder(postorder.begin() + left_inorder.size(), postorder.end()-1);\n\n // Get left and right subtrees\n mid->left = traversal(left_inorder, left_postorder);\n mid->right = traversal(right_inorder, right_postorder);\n return mid;\n };\n};", "memory": "106360" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n return traversal(inorder, postorder);\n };\n\n TreeNode* traversal(vector<int> inorder, vector<int> postorder) {\n // empty postorder vector (end condition)\n if (postorder.size()==0) {\n return nullptr;\n }\n\n // Last entry of postorder is the current \"mid\"\n int midValue = postorder[postorder.size()-1];\n TreeNode* mid = new TreeNode(midValue);\n\n if (postorder.size()==1) {\n return mid;\n }\n\n // Find midValue in inorder vector\n // inorder vector = [left tree] + mid + [right tree]\n int midIndex = 0;\n while (midIndex < inorder.size()) {\n if (inorder[midIndex] == midValue) {\n break;\n }\n midIndex++;\n }\n\n // Get left tree's vectors and right tree's vectors\n vector<int> left_inorder(inorder.begin(), inorder.begin() + midIndex);\n vector<int> right_inorder(inorder.begin() + midIndex + 1, inorder.end() );\n vector<int> left_postorder(postorder.begin(), postorder.begin() + left_inorder.size());\n vector<int> right_postorder(postorder.begin() + left_inorder.size(), postorder.end()-1);\n\n // Get left and right subtrees\n mid->left = traversal(left_inorder, left_postorder);\n mid->right = traversal(right_inorder, right_postorder);\n return mid;\n };\n};", "memory": "106360" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
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* make(vector<int> postorder, int &idx, vector<int> inorder, int start, int end){\n TreeNode* node = new TreeNode(postorder[idx]);\n if(start == end)\n return node;\n int k = start;\n while(postorder[idx]!=inorder[k]) ++k;\n if(k!=end)\n node->right = make(postorder, --idx, inorder, k+1, end);\n if(k!=start)\n node->left = make(postorder, --idx, inorder, start, k-1);\n return node;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if(!postorder.size())\n return NULL;\n int idx = postorder.size()-1;\n return make(postorder, idx, inorder, 0, inorder.size()-1);\n }\n};", "memory": "111325" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
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 preInd = 0;\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n // preInd = postorder.size()-1;\n return createTree(inorder, postorder, 0, inorder.size()-1);\n \n }\n\n TreeNode* createTree(vector<int> inorder , vector<int> &postorder, int start, int end){\n // if((start>end) || (postorder.size()==0))return NULL;\n if(start>end)return NULL;\n\n // if(preInd<0) return NULL;\n // cout<<preInd<<endl;\n TreeNode* root = new TreeNode(postorder.back());\n postorder.pop_back();\n int pos;\n for(int i=start;i<end;i++){\n if(inorder[i]==root->val){\n pos = i;\n break;\n }\n }\n root->right = createTree(inorder, postorder, pos+1, end);\n root->left = createTree(inorder, postorder, start,pos-1);\n return root;\n }\n};", "memory": "112566" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
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 findPos(int x,vector<int>& inorder){\n for(int i=0;i<inorder.size();i++){\n if(x==inorder[i]) return i;\n\n }\n return -1;\n}\nTreeNode*solve(int& index,int start,int end,vector<int>inorder,vector<int>&postorder){\n if(index<0) return NULL;\n if(start>end) return NULL;\n TreeNode*root=new TreeNode(postorder[index]);\n int find=findPos(postorder[index],inorder);\n index--;\n root->right=solve(index,find+1,end,inorder,postorder);\n root->left=solve(index,start,find-1,inorder,postorder);\n return root;\n}\n\n\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int index=postorder.size()-1;\n int start=0;\n int end=inorder.size()-1;\n return solve(index,start,end,inorder,postorder); \n }\n};", "memory": "113808" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* build(vector<int>& in, int inStart, int inEnd, vector<int> post, int postStart, int postEnd) {\n if(inStart>inEnd || postStart>postEnd)\n return NULL;\n\n TreeNode* root = new TreeNode(post[postEnd]);\n\n int i = inStart;\n while(i<=inEnd){\n if(in[i]==post[postEnd]) break;\n i++;\n }\n\n int leftCnt = i-inStart;\n int rightCnt = inEnd-i;\n\n root->right = build(in, i+1, inEnd, post, postEnd-rightCnt,postEnd-1);\n root->left = build(in, inStart, i-1,post, postStart, postStart+leftCnt-1);\n\n return root; \n }\n\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n int n = inorder.size();\n\n return build(inorder,0,n-1,postorder,0,n-1);\n }\n};", "memory": "115049" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
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* buildTreeIP(vector<int> inorder,int startIn,int endIn,vector<int>& postorder,int startPost,int endPost){\n if (startIn >= endIn || startPost >= endPost) return nullptr;\n\n int rootValue = postorder[endPost - 1];\n TreeNode* root = new TreeNode(rootValue);\n \n // Find the root's index in the inorder array\n int rootIndexInorder = startIn;\n while (inorder[rootIndexInorder] != rootValue) {\n rootIndexInorder++;\n }\n \n // The size of the left subtree\n int leftSubtreeSize = rootIndexInorder - startIn;\n \n // Recursively build the left and right subtrees\n root->left = buildTreeIP(inorder, startIn, rootIndexInorder, postorder, startPost, startPost + leftSubtreeSize);\n root->right = buildTreeIP(inorder, rootIndexInorder + 1, endIn, postorder, startPost + leftSubtreeSize, endPost - 1);\n \n return root;\n }\n\n\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n if(inorder.size() == 0) return nullptr;\n return buildTreeIP(inorder,0,inorder.size(),postorder,0,postorder.size()); \n } \n};", "memory": "116290" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map <int, int> hash;\n int index = 0;\n TreeNode* arrayToTree(vector<int> postorder, int left, int right) {\n if(left > right) {\n return NULL;\n }\n\n int rootval = postorder[index--];\n TreeNode* root = new TreeNode(rootval);\n\n root->right = arrayToTree(postorder, hash[rootval] + 1, right);\n root->left = arrayToTree(postorder, left, hash[rootval] -1);\n\n return root;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n for(int i = 0; i < inorder.size();i++){\n hash[inorder[i]] = i;\n }\n index = postorder.size()-1;\n return arrayToTree(postorder, 0, postorder.size()-1);\n }\n};", "memory": "117531" }
106
<p>Given two integer arrays <code>inorder</code> and <code>postorder</code> where <code>inorder</code> is the inorder traversal of a binary tree and <code>postorder</code> is the postorder traversal of the same tree, construct and return <em>the binary tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] <strong>Output:</strong> [3,9,20,null,null,15,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> inorder = [-1], postorder = [-1] <strong>Output:</strong> [-1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= inorder.length &lt;= 3000</code></li> <li><code>postorder.length == inorder.length</code></li> <li><code>-3000 &lt;= inorder[i], postorder[i] &lt;= 3000</code></li> <li><code>inorder</code> and <code>postorder</code> consist of <strong>unique</strong> values.</li> <li>Each value of <code>postorder</code> also appears in <code>inorder</code>.</li> <li><code>inorder</code> is <strong>guaranteed</strong> to be the inorder traversal of the tree.</li> <li><code>postorder</code> is <strong>guaranteed</strong> to be the postorder traversal of the tree.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map <int, int> hash;\n int index = 0;\n TreeNode* arrayToTree(vector<int> postorder, int left, int right) {\n if(left > right) {\n return NULL;\n }\n\n int rootval = postorder[index--];\n TreeNode* root = new TreeNode(rootval);\n\n root->right = arrayToTree(postorder, hash[rootval] + 1, right);\n root->left = arrayToTree(postorder, left, hash[rootval] -1);\n\n return root;\n }\n TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n for(int i = 0; i < inorder.size();i++){\n hash[inorder[i]] = i;\n }\n index = postorder.size()-1;\n return arrayToTree(postorder, 0, postorder.size()-1);\n }\n};", "memory": "118773" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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 */\n \nclass Solution {\npublic:\n vector<vector<int>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>> vec;\n if(root==NULL)\n return vec;\n queue<TreeNode*> q;\n q.push(root);\n int s;\n int i;\n while(!q.empty())\n {\n s = q.size();\n vector<int> currLevel;\n for(i = 0; i<s; i++)\n {\n TreeNode *node = q.front();\n q.pop();\n\n if(node->left!=NULL)\n {\n q.push(node->left);\n }\n \n if(node->right!=NULL)\n {\n q.push(node->right);\n }\n\n node->left = node->right = nullptr;\n \n currLevel.emplace_back(move(node->val));\n }\n\n vec.emplace_back(move(currLevel));\n\n }\n reverse(vec.begin(), vec.end());\n return vec;\n }\n};", "memory": "10800" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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\n int height(TreeNode* a)\n {\n return (!a) ? 0: max(height(a->left),height(a->right))+1;\n }\n\npublic:\n\n vector<vector<int>> levelOrderBottom(TreeNode* a) {\n int h = height(a);\n vector<vector<int>> V(h,vector<int>{});\n\n DFS(a,h-1,V);\n return V; \n }\n\n void DFS(TreeNode* a , int h ,vector<vector<int>>& V )\n {\n if(!a)return ;\n\n V[h].push_back(a->val);\n DFS(a->left,h-1,V);\n DFS(a->right,h-1,V);\n }\n\n};", "memory": "13500" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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 int depth(TreeNode *root) {\n if (!root) return 0;\n return max(depth(root->left),depth(root->right))+1;\n }\n\n void levelOrder(vector<vector<int>> &ans, TreeNode *node, int level) {\n if (!node) return;\n ans[level].push_back(node->val);\n levelOrder(ans,node->left,level-1);\n levelOrder(ans,node->right,level-1);\n }\n\n vector<vector<int>> levelOrderBottom(TreeNode* root) {\n int d = depth(root);\n vector<vector<int>> ans(d,vector<int> {});\n levelOrder(ans,root,d-1);\n return ans;\n }\n};", "memory": "13600" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>> levels;\n \n int maxHeight = getmaxHeight(root);\n levels.resize(maxHeight);\n \n \n levelOrderBottomHelper(root,levels,maxHeight);\n return levels;\n \n }\n int getmaxHeight(TreeNode*root){\n if(root == nullptr){\n return 0;\n }\n int left = getmaxHeight(root->left)+1;\n int right = getmaxHeight(root->right)+1;\n if(left>right){\n return left;\n }\n return right;\n \n \n }\n \n \n\n void levelOrderBottomHelper(TreeNode* root,vector<vector<int>>&levels,int height) {\n if(root == nullptr){\n return;\n }\n levels[height-1].push_back(root->val);\n height--;\n levelOrderBottomHelper(root->left,levels,height);\n levelOrderBottomHelper(root->right,levels,height);\n \n \n \n \n }\n};", "memory": "13700" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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 void nlevel(TreeNode *root,int n,int i,vector<vector<int>>&v){\n if(root==NULL) return ;\n if(i==n){\n v[i-1].push_back(root->val);}\n nlevel(root->left,n,i+1,v);\n nlevel(root->right,n,i+1,v);}\n\n int level (TreeNode *root){\n if(root==NULL)return 0;\n return 1+max(level(root->left),level(root->right));}\n\n vector<vector<int>> levelOrder(TreeNode* root) {\n int n = level(root);\n vector<vector<int>> v(n);\n for(int i =1;i<=n;i++){\n nlevel(root,i,1,v);\n } \n return v;\n }\n vector<vector<int>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>>ans = levelOrder(root);\n reverse(ans.begin(),ans.end());\n return ans; \n }\n};", "memory": "13800" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>> result;\n if(root==NULL){\n return result;\n }\n queue<TreeNode*> q;\n q.push(root);\n\n while(!q.empty()){\n int size=q.size();\n vector<int> ans(size);\n for(int i=0;i<size;i++){\n TreeNode* frontnode=q.front();\n q.pop();\n ans[i]=frontnode->val;\n\n if(frontnode->left)\n q.push(frontnode->left);\n \n if(frontnode->right)\n q.push(frontnode->right);\n }\n result.push_back(ans);\n }\n reverse(result.begin(),result.end());\n return result;\n }\n};", "memory": "13800" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>> res;\n if(!root) return res;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int level = q.size();\n vector<int> v;\n for(int i = 0; i<level; i++){\n TreeNode* node = q.front();\n q.pop();\n v.push_back(node->val);\n if(node->left){\n q.push(node->left);\n }\n if(node->right){\n q.push(node->right);\n }\n }\n res.insert(res.begin(), v);\n }\n return res;\n }\n};", "memory": "13900" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n\n vector<vector<int>> res;\n if(!root)\n return res;\n\n queue<TreeNode*> q;\n q.push(root);\n\n while(!q.empty())\n {\n int cnt=q.size();\n vector<int> temp;\n\n for(int i=0;i<cnt;i++)\n {\n TreeNode* curr = q.front();\n q.pop();\n\n temp.push_back(curr->val);\n \n if(curr->left)\n q.push(curr->left);\n if(curr->right)\n q.push(curr->right);\n }\n res.push_back(temp);\n }\n\n reverse(res.begin(),res.end());\n\n return res;\n \n }\n};", "memory": "13900" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n queue<TreeNode*> q;\n q.push(root);\n vector<vector<int>> result;\n while(!q.empty()){\n int q_size = q.size();\n vector<int> v;\n while(q_size > 0){\n auto f = q.front();\n if(f != nullptr){\n v.push_back(f->val);\n q.push(f->left);\n q.push(f->right);\n }\n q.pop();\n q_size -= 1;\n }\n if(v.size() > 0)\n result.insert(result.begin(), v);\n }\n return result;\n\n }\n};", "memory": "14000" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>> res;\n if(!root) return res;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int level = q.size();\n vector<int> v;\n for(int i = 0; i<level; i++){\n TreeNode* node = q.front();\n q.pop();\n v.push_back(node->val);\n if(node->left){\n q.push(node->left);\n }\n if(node->right){\n q.push(node->right);\n }\n }\n res.push_back(v);\n }\n reverse(res.begin(), res.end());\n return res;\n }\n};", "memory": "14000" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>> res;\n if(!root) return res;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int level = q.size();\n vector<int> v;\n for(int i = 0; i<level; i++){\n TreeNode* node = q.front();\n q.pop();\n v.push_back(node->val);\n if(node->left){\n q.push(node->left);\n }\n if(node->right){\n q.push(node->right);\n }\n }\n res.push_back(v);\n }\n reverse(res.begin(), res.end());\n return res;\n }\n};", "memory": "14100" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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": "class Solution {\n public:\n vector<vector<int>> levelOrderBottom(TreeNode* root) {\n if (root == nullptr)\n return {};\n\n vector<vector<int>> ans;\n queue<TreeNode*> q{{root}};\n\n while (!q.empty()) {\n vector<int> currLevel;\n for (int sz = q.size(); sz > 0; --sz) {\n TreeNode* node = q.front();\n q.pop();\n currLevel.push_back(node->val);\n if (node->left)\n q.push(node->left);\n if (node->right)\n q.push(node->right);\n }\n ans.push_back(currLevel);\n }\n\n ranges::reverse(ans);\n return ans;\n }\n};", "memory": "14100" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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\n void solve(TreeNode* root, vector<vector<int>>&ans){\n \n\n if(!root)return;\n\n queue<TreeNode*>q;\n q.push(root);\n\n while(!q.empty()){\n int size = q.size();\n vector<int>res;\n for(int i=0; i<size; i++){\n TreeNode* frontE = q.front();\n q.pop();\n res.push_back(frontE->val);\n if(frontE->left)q.push(frontE->left);\n if(frontE->right)q.push(frontE->right);\n }\n ans.push_back(res);\n }\n \n\n\n }\n vector<vector<int>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>>ans;\n\n solve(root, ans);\n reverse(ans.begin(), ans.end());\n\n return ans;\n }\n};", "memory": "14200" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>> ans;\n queue<TreeNode*> q;\n if(root==NULL)return ans;\n q.push(root);\n while(!q.empty()){\n int size=q.size();\n vector<int> level;\n for(int i=0;i<size;i++){\n TreeNode* node=q.front();\n q.pop();\n if(node->left!=NULL)q.push(node->left);\n if(node->right!=NULL)q.push(node->right);\n level.push_back(node->val);\n }\n ans.push_back(level);\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "14200" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n vector<vector<int>> res;\n if(!root) return res;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int level = q.size();\n vector<int> v;\n for(int i = 0; i<level; i++){\n TreeNode* node = q.front();\n q.pop();\n v.push_back(node->val);\n if(node->left){\n q.push(node->left);\n }\n if(node->right){\n q.push(node->right);\n }\n }\n res.insert(res.begin(), v);\n }\n return res;\n }\n};", "memory": "14300" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> levelOrderBottom(TreeNode* root) {\n if (root == NULL)\n return {};\n\n vector<vector<int>> ans;\n stack<vector<int>> s; // Stack to hold levels\n queue<TreeNode*> q; // Queue for level order traversal\n q.push(root);\n\n while (!q.empty()) {\n int levelSize = q.size();\n vector<int> currLevel; // Vector to hold the current level's values\n\n for (int i = 0; i < levelSize; i++) {\n TreeNode* temp = q.front();\n q.pop();\n currLevel.push_back(\n temp->val); // Add current node's value to the level vector\n\n // Push left and right children to the queue\n if (temp->left)\n q.push(temp->left);\n if (temp->right)\n q.push(temp->right);\n }\n\n // Push the current level onto the stack\n s.push(currLevel);\n }\n\n // Pop levels from the stack to get them in bottom-up order\n while (!s.empty()) {\n ans.push_back(s.top());\n s.pop();\n }\n\n return ans;\n }\n};", "memory": "14300" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n if (!root) return {};\n queue<TreeNode*>q1, q2;\n stack<vector<int>>s;\n vector<vector<int>>res;\n TreeNode *elem;\n int i;\n q1.push(root);\n s.push({root->val});\n while (q1.size()) {\n elem = q1.front();\n q1.pop();\n if (elem->left)\n q2.push(elem->left);\n if (elem->right)\n q2.push(elem->right);\n if (q1.size() == 0 && q2.size()) {\n vector<int>v(q2.size());\n i = 0;\n while (q2.size()) {\n elem = q2.front();\n q2.pop();\n v[i++] = elem->val;\n q1.push(elem);\n }\n s.push(v);\n }\n }\n while (s.size()) {\n res.push_back(s.top());\n s.pop();\n }\n return res;\n }\n};", "memory": "14400" }
107
<p>Given the <code>root</code> of a binary tree, return <em>the bottom-up level order traversal of its nodes&#39; values</em>. (i.e., from left to right, level by level from leaf to root).</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> [[15,7],[9,20],[3]] </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>> levelOrderBottom(TreeNode* root) {\n queue<pair<TreeNode*,int>> q;\n vector<vector<int>> ans;\n if(root == NULL){\n return ans;\n }\n stack<pair<int,int>> st;\n int h = -1;\n q.push({root,0});\n while(!q.empty()){\n TreeNode* node = q.front().first;\n int temp = node->val;\n int lebel = q.front().second;\n q.pop();\n\n if(lebel>h){\n h++;\n vector<int> v;\n ans.push_back(v);\n }\n\n ans[lebel].push_back(temp);\n if(node->left != NULL){\n q.push({node->left,lebel+1});\n }\n if(node->right != NULL){\n q.push({node->right,lebel+1});\n }\n }\n vector<vector<int>> main_ans;\n for(int i=ans.size()-1; i>=0; i--){\n main_ans.push_back(ans[i]);\n }\n return main_ans;\n }\n};", "memory": "14400" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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 helper(TreeNode*& root,int low,int high,vector<int>& nums){\n if(low>high){\n return;\n }\n int mid=low+(high-low)/2;\n root=new TreeNode(nums[mid]);\n helper(root->left,low,mid-1,nums);\n helper(root->right,mid+1,high,nums);\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n TreeNode* root;\n helper(root,0,nums.size()-1,nums);\n return root;\n }\n};", "memory": "23020" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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 f(int i,int j,TreeNode*& root,vector<int>& nums){\n if(i>j||i<0||j>nums.size()-1) return;\n int mid=(i+j)/2;\n root=new TreeNode(nums[mid]);\n f(i,mid-1,root->left,nums);\n f(mid+1,j,root->right,nums);\n return;\n }\n\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n TreeNode* root=new TreeNode();\n f(0,nums.size()-1,root,nums);\n return root; \n }\n};", "memory": "23020" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* sortedArrayToBST(vector<int>& nums) {\n int startIndex = nums.size() / 2;\n if (nums.size() == 0) {\n return new TreeNode();\n }\n else if (nums.size() == 1) {\n return new TreeNode(nums[0]);\n }\n else if (nums.size() == 2) {\n auto left = new TreeNode(nums[0]);\n return new TreeNode(nums[1], left, nullptr);\n }\n\n vector<int> numsLeft(nums.size() / 2);\n vector<int> numsRight(nums.size() / 2);\n int i = 0;\n for (i = 0; i < startIndex; i++)\n {\n numsLeft[i] = nums[i];\n cout << numsLeft[i] << \" \";\n }\n numsLeft.resize(i);\n cout << endl;\n int rightIdx = 0;\n for (int i = startIndex + 1; i < nums.size(); i++, rightIdx++)\n {\n numsRight[rightIdx] = nums[i];\n cout << numsRight[rightIdx] << \" \";\n }\n numsRight.resize(rightIdx);\n cout << endl;\n\n const auto head = new TreeNode(nums[startIndex],\n sortedArrayToBST(numsLeft),\n sortedArrayToBST(numsRight));\n\n return head;\n }\n};", "memory": "27061" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* sortedArrayToBST(vector<int>& nums) {\n if (nums.empty())\n return nullptr;\n int mid = nums.size()/2;\n TreeNode* node = new TreeNode(nums[mid]);\n if (nums.size() == 1)\n return node;\n else if (nums.size() == 2) {\n TreeNode* leftNode = new TreeNode(nums[0]);\n node->left = leftNode;\n }\n else {\n vector<int> subL(nums.begin(), nums.begin() + mid );\n vector<int> subR(nums.begin() + mid + 1, nums.end());\n node->left = sortedArrayToBST(subL);\n node->right = sortedArrayToBST(subR);\n }\n return node; \n }\n};", "memory": "27061" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* createTree(vector<int> nums, int i, int j) {\n\n int mid = (i + j) / 2;\n TreeNode* root = new TreeNode(nums[mid]);\n if (i < mid - 1) {\n root->left = createTree(nums, i, mid - 1);\n } else if(i==mid-1) {\n root->left = new TreeNode(nums[mid - 1]);\n }\n if (j > mid + 1) {\n root->right = createTree(nums, mid + 1, j);\n } else if(j==mid+1) {\n root->right = new TreeNode(nums[mid + 1]);\n }\n return root;\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int i = 0;\n int j = nums.size() - 1;\n TreeNode* root = createTree(nums, i, j);\n return root;\n }\n};", "memory": "31103" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void MakeBst(vector<int>temp,TreeNode* root,int lb, int ub,int mid){\n if(lb>=ub)return;\n else if (lb == mid){\n TreeNode* ptr1 = new TreeNode(temp[ub]);\n root->right = ptr1;\n return;\n }\n else if(ub == mid){\n TreeNode* ptr1 = new TreeNode(temp[lb]);\n root->left = ptr1;\n return;\n\n }\n int midi = (lb+mid)/2;\n TreeNode* ptr1 = new TreeNode(temp[midi]);\n root->left = ptr1;\n MakeBst(temp,ptr1,lb,mid-1,midi);\n int pidi = (mid+1+ub)/2;\n TreeNode*ptr2 = new TreeNode(temp[pidi]);\n root->right = ptr2;\n MakeBst(temp,ptr2,mid+1,ub,pidi);\n\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n \n int size = nums.size();\n int mid = size/2;\n TreeNode* root = new TreeNode(nums[mid]);\n MakeBst(nums,root,0,size-1,mid);\n return root;\n\n \n }\n};", "memory": "35144" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* convert(vector<int>nums, int l, int r)\n {\n if(l>r)\n return NULL;\n int mid = (l+r)/2;\n TreeNode *root = new TreeNode(nums[mid]);\n root->left = NULL;\n root->right = NULL;\n if(l<=mid-1)\n root->left = convert(nums, l, mid-1);\n if(r>=mid+1)\n root->right = convert(nums, mid+1, r);\n return root;\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int n = nums.size();\n TreeNode *root = convert(nums, 0, n-1);\n return root;\n }\n};", "memory": "39185" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n \n \n\n TreeNode* help(vector<int> nums,int left,int right){\n if(left>right){\n return NULL;\n }\n int mid=left+ (right-left)/2;\n TreeNode* head =new TreeNode(nums[mid]);\n if(mid-1>=left){\n head->left=help(nums,left,mid-1);\n }\n if(mid+1<=right){\n head->right=help(nums,mid+1,right);\n }\n \n \n return head;\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n return help(nums,0,nums.size()-1);\n }\n};", "memory": "43226" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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 binaryBuild(vector<int> nums, int l, int r, TreeNode* elem){\n if(l <= r){\n int mid = (l + r) / 2;\n\n elem -> val = nums[mid];\n\n if(l <= mid - 1){\n elem -> left = new TreeNode();\n binaryBuild(nums, l, mid - 1, elem -> left);\n }\n\n if(mid + 1 <= r){\n elem -> right = new TreeNode();\n binaryBuild(nums, mid + 1, r, elem -> right);\n }\n }\n }\n\n TreeNode* sortedArrayToBST(vector<int>& nums) { \n binaryBuild(nums, 0, nums.size() - 1, root);\n\n return root;\n }\n};", "memory": "47268" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n TreeNode* sortedArrayToBST(vector<int> &nums) {\n int middle = nums.size() / 2; \n int ele = nums[nums.size()/2];\n TreeNode *root = new TreeNode(ele);\n if(0 <= middle -1){\n createTree(root, nums, 0, middle-1);\n }\n if(middle + 1 <= nums.size() - 1){\n createTree(root, nums, middle+1, nums.size() - 1);\n }\n return root;\n }\n\n void createTree(TreeNode *root, vector<int> nums, int low, int high){\n int ele = nums[(low+high)/2];\n int middle = (low + high) / 2;\n TreeNode *node = new TreeNode(ele);\n TreeNode *curr = root;\n TreeNode *parent = nullptr;\n while(curr != NULL){\n parent = curr;\n if(ele < curr->val){\n curr = curr->left;\n }\n else{\n curr = curr->right;\n }\n }\n if(ele < parent->val){\n parent->left = node;\n }\n else{\n parent->right = node;\n }\n\n if(middle-1 >= low){\n createTree(root, nums, low, middle-1);\n }\n if(middle + 1 <= high){\n createTree(root, nums, middle+1, high);\n }\n }\n};", "memory": "47268" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void traverse(TreeNode* root, vector<int> nums, int l, int r){\n if(l>r)\n return;\n int mid = (l+r)/2;\n root->val = nums[mid];\n if(l<=mid-1){\n root->left = new TreeNode();\n traverse(root->left, nums, l, mid-1);\n }\n if(mid+1<=r){\n root->right = new TreeNode();\n traverse(root->right, nums, mid+1, r);\n }\n }\n\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int n = nums.size();\n TreeNode *root = new TreeNode();\n traverse(root, nums, 0, n-1);\n return root;\n }\n};", "memory": "51309" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* helper(int low,int high,vector<int> nums,bool left){\n if(low>high or low<0 or high>=nums.size()) return NULL;\n if(low==high) return new TreeNode(nums[low]);\n int size = high-low+1;\n TreeNode* root;\n if(size%2==0){\n if(left){\n int mid = (low+high)/2;\n mid++;\n root = new TreeNode(nums[mid]);\n root->left = helper(low,mid-1,nums,true);\n root->right = helper(mid+1,high,nums,false);\n }else{\n int mid = (low+high)/2;\n root = new TreeNode(nums[mid]);\n root->left = helper(low,mid-1,nums,true);\n root->right = helper(mid+1,high,nums,false);\n }\n }else{\n int mid = (low+high)/2;\n root = new TreeNode(nums[mid]);\n root->left = helper(low,mid-1,nums,true);\n root->right = helper(mid+1,high,nums,false);\n }\n return root;\n\n } \n\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int mid = (nums.size()-1)/2;\n TreeNode* root = new TreeNode(nums[mid]);\n\n root->left = helper(0,mid-1,nums,true);\n root->right = helper(mid+1,nums.size()-1,nums,true);\n\n return root;\n }\n};", "memory": "55350" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* helper(vector<int> nums,int p,int q){\n int l=q-p+1;\n if(l<=0){\n return NULL;\n }\n\n if(l==1){\n TreeNode* ans=new TreeNode(nums[p]);\n return ans;\n }else if(l%2==1){\n int mid=p+(l-1)/2;\n TreeNode* ans=new TreeNode(nums[mid]);\n ans->left=helper(nums,p,mid-1);\n ans->right=helper(nums,mid+1,q);\n return ans;\n }else{\n int mid=p+l/2;\n TreeNode* ans=new TreeNode(nums[mid]);\n ans->left=helper(nums,p,mid-1);\n ans->right=helper(nums,mid+1,q);\n return ans;\n }\n return NULL;\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n TreeNode* ans=helper(nums,0,nums.size()-1);\n return ans;\n }\n};", "memory": "59391" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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 */\nTreeNode* right(TreeNode* node, vector<int> nums, int start, int end){\n if(start==end){\n TreeNode* temp = new TreeNode(nums[start]);\n return temp;\n }\n else if(start>end){\n return NULL;\n }\n else{\n int l = (start+end)/2;\n TreeNode* temp = new TreeNode(nums[l]);\n temp->left = right(temp, nums, start, l-1);\n temp->right = right(temp, nums, l+1, end);\n return temp;\n\n }\n} \n\nclass Solution {\npublic:\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int l = nums.size();\n int ind = l/2;\n TreeNode* node = new TreeNode(nums[ind]);\n node->left = right(node, nums, 0, ind-1);\n node->right = right(node, nums, ind+1, nums.size()-1);\n\n return node;\n }\n};", "memory": "63433" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\nprivate:\n TreeNode* sort(int l, int r, vector<int> nums) {\n\n if (l <= r) {\n\n TreeNode* newNode = new TreeNode();\n\n if (l == r) {\n newNode->val = nums[l];\n }\n\n else {\n int mid = l + (r - l) / 2;\n newNode->val = nums[mid];\n newNode->left = sort(l, mid - 1, nums);\n newNode->right = sort(mid + 1, r, nums);\n }\n\n return newNode;\n }\n\n return NULL;\n }\n\npublic:\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n return sort(0, nums.size() - 1, nums);\n }\n};", "memory": "67474" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* makeNode(vector<int> v, int l, int h)\n {\n if(l>h)\n return NULL;\n if(l==h)\n return new TreeNode(v[l],NULL,NULL);\n int mid = (l+h)/2;\n TreeNode *root = new TreeNode(v[mid]);\n root->left = makeNode(v,l,mid-1);\n root->right = makeNode(v,mid+1,h);\n\n return root;\n }\n\n\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n TreeNode *root = NULL; \n root = makeNode(nums,0,nums.size()-1);\n return root;\n \n }\n};", "memory": "71515" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* solve(int first ,int last,vector<int>nums)\n {\n if(first>last) return NULL;\n if(first==last)\n {\n TreeNode *root1=new TreeNode(nums[last]);\n return root1;\n }\n int mid=(last+first)/2;\n TreeNode *root=new TreeNode(nums[mid]);\n root->left=solve(first,mid-1,nums);\n root->right=solve(mid+1,last,nums);\n return root;\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int first=0;\n int last=nums.size()-1;\n return solve(first,last,nums);\n \n \n }\n};", "memory": "75556" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* solve(int first ,int last,vector<int>nums)\n {\n if(first>last) return NULL;\n if(first==last)\n {\n TreeNode *root1=new TreeNode(nums[last]);\n return root1;\n }\n int mid=first+(last-first)/2;\n TreeNode *root=new TreeNode(nums[mid]);\n root->left=solve(first,mid-1,nums);\n root->right=solve(mid+1,last,nums);\n return root;\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int first=0;\n int last=nums.size()-1;\n return solve(first,last,nums);\n \n \n }\n};", "memory": "75556" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* subtree(vector <int> nums, int s, int e) {\n if(s==e) {\n TreeNode* root = new TreeNode(nums[s]);\n return root;\n }\n if(s > e){\n return nullptr;\n }\n int mid = (s + e)/2;\n TreeNode* root = new TreeNode(nums[mid]);\n root->left = subtree(nums, s, mid - 1);\n root->right = subtree(nums, mid + 1, e);\n return root;\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int n = nums.size();\n \n return subtree(nums, 0, n-1);\n }\n};", "memory": "79598" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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 findmidindex(vector<int>nums,int start,int end){\n while(start<end){\n start++;\n end--;\n }\n return start;\n\n }\n\n TreeNode*Arr2BST(vector <int>nums,int start,int end){\n if(start>end) return NULL;\n if(start==end){\n TreeNode*leaf_node=new TreeNode(nums[start]);\n return leaf_node;\n }\n int mid=findmidindex(nums,start,end);\n TreeNode*new_node=new TreeNode(nums[mid]);\n new_node->left=Arr2BST(nums,start,mid-1);\n new_node->right=Arr2BST(nums,mid+1,end);\n return new_node;\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n size_t start{0};\n size_t end{nums.size()-1};\n return Arr2BST(nums,start,end);\n \n\n \n }\n};", "memory": "99804" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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 void insertRight(int i, int j, TreeNode* genBST, vector<int> nums) {\n if(i > j) return;\n \n cout << i << \" \" << j << endl; \n TreeNode* nwNode = new TreeNode();\n int insertIdx = (j + i) / 2;\n // cout << insertIdx << endl;\n // a check for the index will come here\n if(insertIdx >= 0 && insertIdx < nums.size()) nwNode->val = nums[insertIdx];\n \n genBST->right = nwNode;\n genBST = genBST->right;\n \n // if(j == insertIdx + 1) return;\n \n insertLeft(i, insertIdx - 1, genBST, nums);\n insertRight(insertIdx + 1, j, genBST, nums);\n }\n \n void insertLeft(int i, int j, TreeNode* genBST, vector<int> nums) {\n // cout << i << \" \" << j << endl;\n if(i > j) return;\n \n // cout << i << \" \" << j << endl; \n TreeNode* nwNode = new TreeNode();\n int insertIdx = (j + i + 1) / 2;\n // cout << insertIdx << endl;\n // a check for the index will come here\n if(insertIdx >= 0 && insertIdx < nums.size()) nwNode->val = nums[insertIdx];\n \n genBST->left = nwNode;\n genBST = genBST->left;\n \n // if(i == insertIdx - 1) return;\n \n insertLeft(i, insertIdx - 1, genBST, nums);\n insertRight(insertIdx + 1, j, genBST, nums);\n }\n \n TreeNode* sortedArrayToBST(vector<int>& nums) {\n // select the middle element\n // ->left will have current - i / 2 the element\n // ->right will have n - current / 2 the element\n // this process will be repeated recusively\n int n = nums.size();\n \n TreeNode* genBST = new TreeNode();\n genBST->val = nums[n/2];\n \n insertLeft(0, n/2 - 1, genBST, nums);\n insertRight(n/2 + 1, n - 1, genBST, nums);\n \n return genBST;\n \n }\n};", "memory": "103845" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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 void insertRight(int i, int j, TreeNode* genBST, vector<int> nums) {\n if(i > j) return;\n \n TreeNode* nwNode = new TreeNode();\n int insertIdx = (j + i) / 2;\n if(insertIdx >= 0 && insertIdx < nums.size()) nwNode->val = nums[insertIdx];\n \n genBST->right = nwNode;\n genBST = genBST->right;\n \n insertLeft(i, insertIdx - 1, genBST, nums);\n insertRight(insertIdx + 1, j, genBST, nums);\n }\n \n void insertLeft(int i, int j, TreeNode* genBST, vector<int> nums) {\n if(i > j) return;\n \n TreeNode* nwNode = new TreeNode();\n int insertIdx = (j + i + 1) / 2;\n if(insertIdx >= 0 && insertIdx < nums.size()) nwNode->val = nums[insertIdx];\n \n genBST->left = nwNode;\n genBST = genBST->left;\n \n insertLeft(i, insertIdx - 1, genBST, nums);\n insertRight(insertIdx + 1, j, genBST, nums);\n }\n \n TreeNode* sortedArrayToBST(vector<int>& nums) {\n // select the middle element\n // ->left will have current - i / 2 the element\n // ->right will have n - current / 2 the element\n // this process will be repeated recusively\n int n = nums.size();\n \n TreeNode* genBST = new TreeNode();\n genBST->val = nums[n/2];\n \n insertLeft(0, n/2 - 1, genBST, nums);\n insertRight(n/2 + 1, n - 1, genBST, nums);\n \n return genBST;\n \n }\n};", "memory": "103845" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</li> </ul>
3
{ "code": "class Solution \n{\npublic:\n void insert(TreeNode* root, int val)\n {\n if (!root)\n return;\n if (val > root->val)\n {\n if (!root->right)\n {\n TreeNode* node = new TreeNode(val);\n root->right = node;\n return;\n }\n else\n {\n insert(root->right, val);\n }\n }\n else\n {\n if (!root->left)\n {\n TreeNode* node = new TreeNode(val);\n root->left = node;\n return;\n }\n else\n {\n insert(root->left, val);\n }\n }\n return;\n }\n TreeNode* root;\n void rec(vector<int> nums, int start, int end)\n {\n if (start > end)\n return;\n int mid = (start + end) / 2;\n insert(root, nums[mid]);\n rec(nums, start, mid - 1);\n rec(nums, mid + 1, end);\n }\n TreeNode* sortedArrayToBST(vector<int>& nums)\n {\n root = new TreeNode;\n root->val = nums[nums.size() / 2];\n rec(nums, 0, nums.size() / 2 - 1);\n rec(nums, nums.size() / 2 + 1, nums.size() - 1);\n return root;\n }\n};", "memory": "107886" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* sortedArrayToBST(vector<int>& nums) {\n int size = nums.size();\n return Pre_build_tree(nums, 0, size-1);\n }\n TreeNode* Pre_build_tree(vector<int> nums, int start, int end)\n { \n\n if(start > end) return NULL;\n int now = (start + end)/2;\n TreeNode* head = new TreeNode(nums[now]);\n head-> left = Pre_build_tree(nums, start, now-1);\n head -> right = Pre_build_tree(nums, now+1, end);\n return head;\n\n }\n};\n", "memory": "111928" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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:\nTreeNode *convert(vector<int> ar,int l, int r)\n{\n if (l > r)\n return NULL;\n int mid = (l + r) / 2;\n TreeNode *root = new TreeNode(ar[mid]);\n TreeNode *leftRoot = convert(ar,l, mid - 1);\n TreeNode *rightRoot = convert(ar,mid + 1, r);\n root->left = leftRoot;\n root->right = rightRoot;\n return root;\n}\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int l=0;\n int r=nums.size()-1;\n TreeNode* root = convert(nums,l,r);\n return root;\n\n }\n};", "memory": "111928" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* Pre_build_tree(vector<int> nums, int start, int end)\n { \n\n if(start > end) return NULL;\n int now = (start + end)/2;\n TreeNode* head = new TreeNode(nums[now]);\n head-> left = Pre_build_tree(nums, start, now-1);\n head -> right = Pre_build_tree(nums, now+1, end);\n return head;\n\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n if(nums.size()==0) return NULL;\n return Pre_build_tree(nums, 0, nums.size()-1);\n }\n\n};\n", "memory": "115969" }
108
<p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> <pre> <strong>Input:</strong> nums = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> [3,1] <strong>Explanation:</strong> [1,null,3] and [3,1] are both height-balanced BSTs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</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* sol(vector<int> nums, int i, int j){\n if(i >= j)return NULL;\n int mid = (i+j)/2;\n\n TreeNode* root = new TreeNode(nums[mid]);\n root->left = sol(nums, i, mid);\n root->right = sol(nums, mid+1, j);\n\n return root;\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n int n = nums.size();\n return sol(nums, 0, n);\n }\n};", "memory": "115969" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void func(TreeNode* &temp, ListNode* head){\n if(head==NULL){\n return;\n }\n if(head->next==NULL){\n temp = new TreeNode(head->val);\n return;\n }\n ListNode* slow = head;\n ListNode* fast = head;\n ListNode* prev = NULL;\n while(fast!=NULL && fast->next!=NULL){\n prev = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n prev->next = NULL;\n temp = new TreeNode(slow->val);\n func(temp->left, head);\n func(temp->right, slow->next);\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n TreeNode* root = NULL;\n func(root, head);\n return root;\n }\n};", "memory": "26400" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n void solve(TreeNode* &root,vector<int> &arr,int s,int e){\n int mid = (s+e)/2;\n if(s>e){\n return;\n }\n root = new TreeNode(arr[mid]);\n solve(root->left,arr,s,mid-1);\n solve(root->right,arr,mid+1,e);\n }\n TreeNode* sortedListToBST(ListNode* head) {\n if(head == NULL){\n return NULL;\n }\n vector<int> arr;\n while(head != NULL){\n arr.push_back(head->val);\n head = head->next;\n }\n TreeNode* root = nullptr;\n int s = 0;\n int e = arr.size()-1;\n\n solve(root,arr,s,e);\n return root;\n \n }\n};", "memory": "26500" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *midElement(ListNode *head) {\n ListNode *slow = head;\n ListNode *fast = head->next;\n while(fast != NULL && fast->next != NULL) {\n fast = fast->next->next;\n slow = slow->next;\n }\n return slow;\n }\n void recur(ListNode *head, TreeNode *&root) {\n if(head == NULL) {root = NULL; return;}\n ListNode *mid = midElement(head);\n root = new TreeNode(mid->val);\n if(mid == head) {\n recur(head->next, root->right);\n return;\n }\n ListNode *temp = head;\n while(temp->next != mid) {\n temp = temp->next;\n }\n temp->next = NULL;\n recur(head, root->left);\n recur(mid->next, root->right);\n }\n TreeNode* sortedListToBST(ListNode* head) {\n if(head == NULL) return NULL;\n TreeNode *root;\n recur(head, root);\n return root;\n }\n};", "memory": "26600" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void f( TreeNode* &root, vector<int> &in, int s, int e )\n {\n if( s > e ) \n {\n root = NULL;\n return;\n }\n\n int mid = s + ( e - s )/2;\n root = new TreeNode( in[ mid ] );\n\n cout << root -> val << endl;\n\n f( root -> left, in, s, mid - 1 );\n f( root -> right, in, mid + 1, e );\n }\n TreeNode* sortedListToBST(ListNode* head) \n {\n if( !head ) return NULL;\n\n vector<int> inorder;\n ListNode* temp = head;\n while( temp )\n {\n inorder.push_back( temp -> val );\n temp = temp -> next;\n }\n\n int s = 0, e = inorder.size() - 1;\n int mid = s + ( e - s )/2;\n TreeNode* ans = new TreeNode( inorder[ mid ] );\n\n f( ans -> left, inorder, s, mid - 1 );\n f( ans -> right, inorder, mid + 1, e );\n return ans;\n }\n};", "memory": "26700" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void f( TreeNode* &root, ListNode* s, ListNode* e )\n {\n if( !s || !e || e -> next == s ) \n {\n root = NULL;\n return;\n }\n\n ListNode* slow = s;\n ListNode* fast = s;\n ListNode* prev = NULL;\n while( fast != e && fast -> next != e )\n {\n prev = slow;\n slow = slow -> next;\n fast = fast -> next -> next;\n }\n ListNode* mid = slow;\n root = new TreeNode( mid -> val );\n\n f( root -> left, s, prev);\n f( root -> right, mid -> next, e );\n }\n TreeNode* sortedListToBST(ListNode* head) \n {\n if( !head ) return NULL;\n ListNode* temp = head;\n while( temp -> next ) temp = temp -> next;\n\n TreeNode* ans;\n f( ans, head, temp );\n return ans;\n }\n};", "memory": "26700" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(TreeNode* &root,vector<int> &arr,int s,int e){\n int mid = (s+e)/2;\n if(s>e){\n return;\n }\n root = new TreeNode(arr[mid]);\n solve(root->left,arr,s,mid-1);\n solve(root->right,arr,mid+1,e);\n }\n TreeNode* sortedListToBST(ListNode* head) {\n if(head == NULL){\n return NULL;\n }\n vector<int> arr;\n while(head != NULL){\n arr.push_back(head->val);\n head = head->next;\n }\n TreeNode* root = nullptr;\n int s = 0;\n int e = arr.size()-1;\n\n solve(root,arr,s,e);\n return root;\n \n }\n};", "memory": "26800" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(TreeNode* &root,vector<int> &arr,int s,int e){\n int mid = (s+e)/2;\n if(s>e){\n return;\n }\n root = new TreeNode(arr[mid]);\n solve(root->left,arr,s,mid-1);\n solve(root->right,arr,mid+1,e);\n }\n TreeNode* sortedListToBST(ListNode* head) {\n if(head == NULL){\n return NULL;\n }\n vector<int> arr;\n while(head != NULL){\n arr.push_back(head->val);\n head = head->next;\n }\n TreeNode* root = nullptr;\n int s = 0;\n int e = arr.size()-1;\n\n solve(root,arr,s,e);\n return root;\n \n }\n};", "memory": "26800" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void tree(TreeNode* &root,int start,int end,vector<int> &nums)\n {\n if(start>end)\n {\n return;\n }\n int mid=start + (end-start)/2;\n root=new TreeNode(nums[mid]);\n tree(root->left,start,mid-1,nums);\n tree(root->right,mid+1,end,nums);\n }\n \n \n TreeNode* sortedListToBST(ListNode* head) {\n vector<int> nums;\n while(head)\n {\n nums.push_back(head->val);\n head=head->next;\n }\n TreeNode* root=NULL;\n tree(root,0,nums.size()-1,nums);\n return root;\n }\n};", "memory": "26900" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* divideAndConquer(vector<TreeNode*> &list, int start, int end) {\n if(start > end) return nullptr;\n \n int mid = start + (end - start)/2;\n\n if(mid < 0 || mid >= list.size()) return nullptr;\n\n TreeNode *root = list[mid];\n root->left = divideAndConquer(list, start, mid - 1); //mid - 1 could be less than start\n root->right = divideAndConquer(list, mid + 1, end); //mid + 1 could be greater than end\n\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n if(!head) return nullptr;\n\n vector<TreeNode*> list;\n while(head) {\n list.emplace_back(new TreeNode(head->val));\n head = head->next;\n }\n\n return divideAndConquer(list, 0, list.size() - 1);\n }\n};", "memory": "27000" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int height(TreeNode *tree)\n {\n if (!tree)\n return -1;\n return max(height(tree->left), height(tree->right)) + 1;\n }\n TreeNode *rotateLeft(TreeNode *x)\n {\n TreeNode *y = x->right;\n TreeNode *t2 = y->left;\n x->right = t2;\n y->left = x;\n return y;\n }\n TreeNode *rotateRight(TreeNode *x)\n {\n TreeNode *y = x->left;\n TreeNode *t2 = y->right;\n x->left = t2;\n y->right = x;\n return y;\n }\n int getBalanceFactor(TreeNode *tree)\n {\n if (!tree)\n return 0;\n return height(tree->left) - height(tree->right);\n }\n void balance(TreeNode *&tree)\n {\n int balanceFactor = getBalanceFactor(tree);\n if (balanceFactor > 0)\n {\n if (getBalanceFactor(tree->left) >= 0)\n {\n tree = rotateRight(tree);\n }\n else\n {\n tree->left = rotateLeft(tree->left);\n tree = rotateRight(tree);\n }\n }\n if (balanceFactor < 0)\n {\n if (getBalanceFactor(tree->right) <= 0)\n {\n tree = rotateLeft(tree);\n }\n else\n {\n tree->right = rotateRight(tree->right);\n tree = rotateLeft(tree);\n }\n }\n }\n void insert(TreeNode *&head, int key)\n {\n if (!head)\n {\n head = new TreeNode{key, nullptr, nullptr};\n return;\n }\n if (head->val > key)\n {\n insert(head->left, key);\n }\n else if (head->val < key)\n {\n insert(head->right, key);\n }\n balance(head);\n }\n TreeNode* sortedListToBST(ListNode* head) {\n ListNode *temp = head;\n TreeNode *tree = nullptr;\n while (temp)\n {\n insert(tree, temp->val);\n temp = temp->next;\n }\n return tree;\n }\n};", "memory": "27100" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid xoayphai(TreeNode*&root)\n{\n TreeNode*b=root;\n TreeNode*tmp=b->left;\n TreeNode*a=tmp->right;\n b->left=a;\n tmp->right=b;\n root=tmp;\n}\nvoid xoaytrai(TreeNode*&root)\n{\n TreeNode*b=root;\n TreeNode*tmp=b->right;\n TreeNode*a=tmp->left;\n b->right=a;\n tmp->left=b;\n root=tmp;\n}\nint dosau(TreeNode*&root)\n{\n if(root==nullptr) return 0;\n return max(dosau(root->left)+1,dosau(root->right)+1);\n}\nvoid xoay(TreeNode*&root)\n{\n if(root!=nullptr)\n {\n if(abs(dosau(root->left)-dosau(root->right))>1)\n {\n if(dosau(root->left)>dosau(root->right))\n {\n if(dosau(root->left->left)>=dosau(root->left->right))\n {\n xoayphai(root);\n }\n else\n {\n xoaytrai(root->left);\n xoayphai(root);\n }\n }\n else\n {\n if(dosau(root->right->right)>=dosau(root->right->left))\n {\n xoaytrai(root);\n }\n else\n {\n xoayphai(root->right);\n xoaytrai(root);\n }\n }\n }\n xoay(root->left);\n xoay(root->right);\n }\n}\n TreeNode* sortedListToBST(ListNode* head) {\n TreeNode*a=nullptr;\n TreeNode*tmp=nullptr;\n while(head!=nullptr)\n {\n if(a==nullptr)\n {\n a=new TreeNode(head->val);\n tmp=a;\n\n }\n else\n {\n tmp->right=new TreeNode(head->val);\n tmp=tmp->right;\n }\n head=head->next;\n }\n if(a!=nullptr)\n {\n while(abs(dosau(a->left)-dosau(a->right))>1)\n {\n xoay(a);\n }\n }\n return a;\n }\n};", "memory": "27400" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n unordered_map<TreeNode*,int> heights;//the given node has not height attribute\n \n TreeNode* leftRotation(TreeNode* A){\n TreeNode* B=A->right;\n A->right=B->left;\n B->left=A;\n updateHeight(A);\n updateHeight(B);\n return B;\n }\n int getHeight(TreeNode* t){\n if(!t) return -1;\n return heights[t];\n }\n int updateHeight(TreeNode* t){\n return heights[t]=max(getHeight(t->left),getHeight(t->right))+1;\n }\n TreeNode* insert(TreeNode* root,int x){\n if(!root){\n auto ans = new TreeNode(x);\n heights[ans]=0;\n return ans;\n }\n else if(x>root->val){\n root->right=insert(root->right,x);\n if(getHeight(root->right)-getHeight(root->left)==2){\n root=leftRotation(root);\n }\n }\n updateHeight(root);\n return root;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n auto p=head;\n TreeNode* root=NULL;\n while(p){\n root=insert(root,p->val);\n p=p->next;\n }\n return root;\n }\n};", "memory": "28000" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*, int> height;\n int get_height(TreeNode* t) {\n if (!t) return -1;\n return height[t];\n }\n\n void update_height(TreeNode* t) {\n height[t] = max(get_height(t->left), get_height(t->right))+1;\n }\n\n TreeNode* rotate(TreeNode* A) {\n auto B = A->right;\n A->right = B->left;\n B->left = A;\n update_height(A);\n update_height(B);\n return B;\n }\n\n TreeNode* insert(TreeNode* root, int val) {\n if (!root) {\n auto newNode = new TreeNode(val);\n height[newNode] = 0;\n return newNode;\n }\n root->right = insert(root->right, val);\n if (abs(get_height(root->left) - get_height(root->right)) >= 2) {\n root = rotate(root);\n }\n update_height(root);\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n auto ptr = head;\n TreeNode* res = nullptr;\n while (ptr) {\n res = insert(res, ptr->val);\n ptr = ptr->next;\n }\n return res;\n }\n};", "memory": "28100" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*, int> height;\n int get_height(TreeNode* t) {\n if (!t) return -1;\n return height[t];\n }\n\n void update_height(TreeNode* t) {\n height[t] = max(get_height(t->left), get_height(t->right))+1;\n }\n\n TreeNode* rotateLeft(TreeNode* A) {\n TreeNode* B = A->right;\n A->right = B->left;\n B->left = A;\n // FIX HEIGHT!!!\n update_height(A);\n update_height(B);\n return B;\n }\n\n TreeNode* insert(TreeNode* root, int val) {\n if (!root) {\n // found the place to insert\n TreeNode* newT = new TreeNode(val);\n height[newT] = 0;\n return newT;\n }\n // insert to right\n root->right = insert(root->right, val);\n if (abs(get_height(root->right) - get_height(root->left)) >= 2) {\n // rotate to the left\n root = rotateLeft(root);\n }\n update_height(root);\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n auto ptr = head;\n TreeNode* res = nullptr;\n while (ptr) {\n res = insert(res, ptr->val);\n ptr = ptr->next;\n }\n return res;\n }\n};", "memory": "28200" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nums;\n unordered_map<TreeNode*,int> heights;\n TreeNode* leftRotation(TreeNode* A){\n TreeNode* B=A->right;\n A->right=B->left;\n B->left=A;\n updateHeight(A);\n updateHeight(B);\n return B;\n }\n int getHeight(TreeNode* t){\n if(!t) return -1;\n return heights[t];\n }\n int updateHeight(TreeNode* t){\n return heights[t]=max(getHeight(t->left),getHeight(t->right))+1;\n }\n TreeNode* insert(TreeNode* root,int x){\n if(!root){\n auto ans = new TreeNode(x);\n heights[ans]=0;\n return ans;\n } \n \n root->right=insert(root->right,x);\n if(getHeight(root->right)-getHeight(root->left)==2){\n root=leftRotation(root);\n }\n updateHeight(root);\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n if (head == nullptr) return nullptr;\n TreeNode* root=nullptr;\n while (head != nullptr) {\n root=insert(root, head->val);\n head = head->next;\n }\n return root;\n }\n};", "memory": "28300" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n TreeNode* sortedListToBST(ListNode* head) {\n // bc\n if(head==NULL) return NULL;\n if(head->next==NULL) return new TreeNode(head->val);\n\n ListNode* slow=head;\n ListNode* fast= head;\n ListNode* prev=NULL;\n while(fast!=NULL && fast->next!=NULL){\n prev=slow;\n fast=fast->next->next;\n slow=slow->next;\n }\n prev->next=NULL;\n TreeNode* root= new TreeNode(slow->val);\n root->left= sortedListToBST(head);\n root->right= sortedListToBST(slow->next);\n return root;\n \n }\n};", "memory": "28700" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* getMiddle(ListNode* left, ListNode* right) {\n ListNode* slow = left;\n ListNode* fast = left;\n while (fast && fast->next && (fast != right) && (fast->next != right)) {\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n }\n TreeNode* toBST(ListNode* left, ListNode* right) {\n if (left == right || (left && right && (left->val >= right->val))) return nullptr;\n\n ListNode* mid = getMiddle(left, right);\n\n if (!mid) return nullptr;\n\n TreeNode* newNode = new TreeNode(mid->val);\n\n newNode->left = toBST(left, mid);\n newNode->right = toBST(mid->next, right);\n return newNode;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n TreeNode* tree = toBST(head, nullptr);\n return tree;\n }\n};", "memory": "28700" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode *helper(ListNode *start, ListNode *end) {\n if (start == end) return NULL;\n ListNode *slow = start, *fast = start;\n while (fast != end && fast->next != end) {\n slow = slow->next;\n fast = fast->next->next;\n }\n TreeNode *root = new TreeNode(slow->val);\n root->left = helper(start, slow);\n root->right = helper(slow->next, end);\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n return helper(head, nullptr);\n }\n};", "memory": "28800" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int getLength(ListNode* head){\n int cnt = 0;\n ListNode* temp = head;\n while(temp != NULL){\n cnt++;\n temp = temp -> next;\n }\n return cnt;\n }\n TreeNode* createBST(ListNode* &head, int n){\n if(head == NULL || n <= 0){\n return NULL;\n }\n\n TreeNode* leftsubtree = createBST(head, n/2);\n\n TreeNode* root = new TreeNode(head -> val);\n head = head -> next;\n root -> left = leftsubtree;\n\n TreeNode* rightsubtree = createBST(head, n - n/2 -1);\n root -> right = rightsubtree;\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n int n = getLength(head);\n TreeNode* root = createBST(head, n);\n return root;\n }\n};", "memory": "28800" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n TreeNode* sortedListToBST(ListNode* head) {\n if (head == nullptr)\n return nullptr;\n if (!head->next)\n return new TreeNode(head->val);\n\n ListNode* mid = findMid(head);\n TreeNode* root = new TreeNode(mid->val);\n root->left = sortedListToBST(head);\n root->right = sortedListToBST(mid->next);\n return root;\n }\n\n private:\n ListNode* findMid(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* slow = head;\n ListNode* fast = head;\n\n while (fast != nullptr && fast->next != nullptr) {\n prev = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n prev->next = nullptr;\n\n return slow;\n }\n};", "memory": "28900" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n\n \n TreeNode* sortedListToBST(ListNode* head) {\n if(!head) return nullptr;\n if(!head->next) return new TreeNode(head->val);\n\n ListNode*mid = nullptr;\n ListNode*prev = nullptr;\n ListNode* slow = head;\n ListNode* fast = head;\n while(fast and fast->next){\n prev = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n mid=slow;\n\n TreeNode*node = new TreeNode(mid->val);\n if(prev) prev->next=nullptr;\n\n node->left = sortedListToBST(head);\n node->right = sortedListToBST(mid->next);\n return node;\n }\n};", "memory": "28900" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\n ListNode* findMid(ListNode* low, ListNode* high) {\n if (!low || low->next == high)\n return low;\n ListNode *slow = low, *fast = low->next;\n\n while (fast != high && fast->next != high) {\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n }\n\npublic:\n TreeNode* sortedListToBST(ListNode* low, ListNode* high = nullptr) {\n if (low == high)\n return nullptr;\n\n ListNode* mid = findMid(low, high);\n TreeNode* node = new TreeNode(mid->val);\n node->left = sortedListToBST(low, mid);\n node->right = sortedListToBST(mid->next, high);\n return node;\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "29000" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nint part(int l,int h)\n{\n return (l+h)/2;\n}\n\nTreeNode*fun(vector<int>&a,int l,int h)\n{\n TreeNode*root=NULL;\n if(l<h)\n {\n int p=part(l,h);\n root=new TreeNode(a[p]);\n root->left=fun(a,l,p-1);\n root->right=fun(a,p+1,h);\n return root;\n }\n if(l==h)\n return new TreeNode(a[l]);\n \n return NULL;\n \n\n}\n TreeNode* sortedListToBST(ListNode* head) {\n if(head==NULL)\n return NULL;\n vector<int>a;\n while(head!=NULL)\n {\n a.push_back(head->val);\n head=head->next;\n }\n return fun(a,0,a.size()-1);\n \n }\n};", "memory": "29300" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n public:\n TreeNode* helper(vector<int>&vec,int i,int j){\n if(i>j) return NULL;\n int mid=(i+j)/2;\n TreeNode* root=new TreeNode(vec[mid]);\n root->left=helper(vec,i,mid-1);\n root->right=helper(vec,mid+1,j);\n\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n vector<int>result;\n ListNode* temp=head;\n while(temp!=NULL){\n result.push_back(temp->val);\n temp=temp->next;\n }\n int i=0,j=result.size()-1;\n return helper(result,i,j);\n /* if(head==NULL) return NULL;\n if(head->next==NULL) return new TreeNode(head->val);\n ListNode* slow=head;\n ListNode* fast=head;\n ListNode* prev=NULL;\n while(fast!=NULL&&fast->next!=NULL){\n prev=slow;\n slow=slow->next;\n fast=fast->next->next;\n }\n TreeNode* root=new TreeNode(slow->val);\n prev->next=NULL;\n root->left=sortedListToBST(head);\n root->right=sortedListToBST(slow->next);\n\n\n return root;*/\n }\n};", "memory": "29300" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* bst(vector<int>&v, int s, int e){\n if(s>e) return nullptr;\n int m = (s + e)/2;\n if((s + e)%2!=0) ++m;\n TreeNode* temp = new TreeNode(v[m]);\n temp->left = bst(v, s, m-1);\n temp->right = bst(v, m+1, e);\n return temp;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n if(!head) return nullptr;\n vector<int>v;\n while(head){\n v.push_back(head->val);\n head = head->next;\n }\n return bst(v, 0, v.size()-1);\n\n }\n};", "memory": "29400" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n public:\n TreeNode* helper(vector<int>&vec,int i,int j){\n if(i>j) return NULL;\n int mid=(i+j)/2;\n TreeNode* root=new TreeNode(vec[mid]);\n root->left=helper(vec,i,mid-1);\n root->right=helper(vec,mid+1,j);\n\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n vector<int>result;\n ListNode* temp=head;\n while(temp!=NULL){\n result.push_back(temp->val);\n temp=temp->next;\n }\n int i=0,j=result.size()-1;\n return helper(result,i,j);\n /* if(head==NULL) return NULL;\n if(head->next==NULL) return new TreeNode(head->val);\n ListNode* slow=head;\n ListNode* fast=head;\n ListNode* prev=NULL;\n while(fast!=NULL&&fast->next!=NULL){\n prev=slow;\n slow=slow->next;\n fast=fast->next->next;\n }\n TreeNode* root=new TreeNode(slow->val);\n prev->next=NULL;\n root->left=sortedListToBST(head);\n root->right=sortedListToBST(slow->next);\n\n\n return root;*/\n }\n};", "memory": "29400" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* toList(const vector<int>& values, int l, int r) {\n if (l >= r) {\n return nullptr;\n } else {\n int mid = (l + r) / 2;\n TreeNode* result = new TreeNode(values[mid]);\n result->left = toList(values, l, mid);\n result->right = toList(values, mid + 1, r);\n return result;\n }\n }\n TreeNode* sortedListToBST(ListNode* head) {\n if (!head) return nullptr;\n vector<int> values;\n while (head) {\n values.push_back(head->val);\n head = head->next;\n }\n return toList(values, 0, values.size());\n }\n};", "memory": "29500" }
109
<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" /> <pre> <strong>Input:</strong> head = [-10,-3,0,5,9] <strong>Output:</strong> [0,-3,9,-10,null,5] <strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in <code>head</code> is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.</li> <li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n TreeNode* buildBST(vector<ListNode*>& nodes, int l, int r) {\n if (l == r) return new TreeNode(nodes[r]->val); \n else if (l > r) return NULL; \n\n int m = (l + r) / 2; \n TreeNode* mid = new TreeNode(nodes[m]->val); \n mid->right = buildBST(nodes, m + 1, r); \n mid->left = buildBST(nodes, l, m - 1);\n return mid; \n }\n\npublic:\n TreeNode* sortedListToBST(ListNode* head) {\n if (!head) return NULL; \n vector<ListNode*> nodes; \n ListNode* front = head; \n while (front) {\n nodes.push_back(front); \n front = front->next; \n }\n return buildBST(nodes, 0, nodes.size() - 1);\n }\n};", "memory": "29600" }