id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
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\n\n TreeNode* createBinaryTree(int value, TreeNode* root){\n if(root == nullptr) return new TreeNode(value);\n if(root && root->val > value)\n root->left = createBinaryTree(value,root->left);\n if(root && root->val < value){\n root->right = createBinaryTree(value,root->right);\n }\n return root;\n }\n\n TreeNode *solve(vector<int>& values, int low, int high, TreeNode* root){\n if(low <= high){\n int mid = low + (high - low)/2;\n root = createBinaryTree(values[mid], root);\n solve(values, low,mid-1,root);\n solve(values,mid+1,high,root);\n }\n\n return root;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n vector<int> values;\n \n ListNode *track;\n track = head;\n while(track){\n values.push_back(track->val);\n track = track->next;\n }\n\n TreeNode *result;\n result = nullptr;\n result = solve(values,0,values.size()-1, result);\n\n return result;\n\n }\n};", "memory": "30100" }
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* solve(unordered_map<int,int> &m ,int start,int end){\n //Base Case\n if(start>end) return NULL;\n int mid=start+(end-start)/2;\n TreeNode* root=new TreeNode(m[mid]);\n root->left=solve(m,start,mid-1);\n root->right=solve(m,mid+1,end);\n return root;\n }\npublic:\n TreeNode* sortedListToBST(ListNode* head) {\n unordered_map<int,int> m;\n int i=0;\n for(i=0;head!=NULL;i++){\n m[i]=head->val;\n head=head->next;\n }\n TreeNode* ans=solve(m,0,i-1);\n return ans;\n }\n};", "memory": "30200" }
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* sortedListToBST(ListNode* head) {\n function<TreeNode*(ListNode*, ListNode*)> makeTree = [&](ListNode* l, ListNode* r) -> TreeNode* {\n if (l == r) return nullptr;\n\n ListNode* mid = l;\n for (ListNode*f = l; f != r && f->next != r;f = f->next->next) mid = mid->next;\n\n auto leftTree = makeTree(l, mid);\n auto rightTree = makeTree(mid->next, r);\n return new TreeNode(mid->val, leftTree, rightTree);\n };\n\n return makeTree(head, nullptr);\n }\n};", "memory": "30300" }
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* sortedListToBST(ListNode* head) {\n if(head == NULL){\n return NULL;\n }\n if(head->next == NULL){\n return new TreeNode(head->val);\n }\n ListNode* dummy = new ListNode ();\n dummy->next = head;\n ListNode* slow = dummy;\n ListNode* fast = dummy;\n while(fast && fast->next){\n fast = fast->next;\n fast = fast->next;\n if(!fast){\n break;\n }\n slow = slow->next;\n }\n TreeNode* cur = new TreeNode(slow->next->val);\n cur->right = sortedListToBST(slow->next->next);\n slow->next = NULL;\n cur->left = sortedListToBST(dummy->next);\n return cur;\n }\n};", "memory": "30400" }
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(unordered_map<int,int> &umap, int start, int end){\n if(start>end) return nullptr;\n int mid = (start + end)/2;\n if((start + end)%2!=0) ++mid;\n TreeNode* temp = new TreeNode(umap[mid]);\n temp->left = bst(umap, start, mid-1);\n temp->right = bst(umap, mid+1, end);\n return temp;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n if(!head) return nullptr;\n unordered_map<int, int> umap;\n int i = 0;\n while(head){\n umap[i] = head->val;\n head = head->next;\n ++i;\n }\n return bst(umap, 0, i-1);\n\n }\n};", "memory": "30500" }
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* sortedListToBST(ListNode* head) {\n if(!head) return nullptr;\n if(!head->next) return new TreeNode(head->val);\n int len=0;\n ListNode* h=head;\n for(;h;h=h->next)len++;\n return sortedListToBST(head, len);\n }\n\n TreeNode* sortedListToBST(ListNode* head, int len){\n if(!head) return nullptr;\n if(!head->next) return new TreeNode(head->val);\n int len1=len/2;\n ListNode *prev=new ListNode(0, head), *h=head;\n for(int i=0;i<len1;i++){\n h=h->next;\n prev=prev->next;\n }\n prev->next=nullptr;\n TreeNode* lchild=sortedListToBST(head,len1);\n TreeNode* rchild=sortedListToBST(h->next,len-len1-1);\n return new TreeNode(h->val, lchild, rchild);\n }\n};", "memory": "30600" }
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* sortedListToBST(ListNode* head) {\n if(head == nullptr) return nullptr;\n if(head->next == nullptr) return new TreeNode(head->val);\n \n vector<int> arr;\n ListNode* node = head;\n while(node != nullptr){\n arr.push_back(node->val);\n node = node->next;\n }\n int n = arr.size(); // n number of nodes\n \n int root_idx = (n - 1) / 2;\n TreeNode* root = new TreeNode(arr[root_idx]);\n vector<TreeNode*> parent;\n vector<char> child_type; // 'L' left 'R' right\n vector<vector<int>> ranges;\n if(root_idx > 0){\n parent.push_back(root);\n child_type.push_back('L');\n ranges.push_back({0, root_idx - 1});\n }if(root_idx != n - 1){\n parent.push_back(root);\n child_type.push_back('R');\n ranges.push_back({root_idx + 1, n - 1});\n }\n while(ranges.size() > 0){\n vector<vector<int>> next_ranges;\n vector<TreeNode*> next_parent;\n vector<char> next_child_type;\n for(int i = 0; i < ranges.size(); ++i){\n TreeNode* parent_node = parent[i];\n char c = child_type[i];\n int beg = ranges[i][0]; // [begin, end] interval\n int end = ranges[i][1];\n int node_idx = (beg + end) / 2;\n TreeNode* new_node = new TreeNode(arr[node_idx]);\n if(c == 'L') parent_node->left = new_node;\n else parent_node->right = new_node;\n if(node_idx > beg){\n next_parent.push_back(new_node);\n next_child_type.push_back('L');\n next_ranges.push_back({beg, node_idx - 1});\n }\n if(node_idx != end){\n next_parent.push_back(new_node);\n next_child_type.push_back('R');\n next_ranges.push_back({node_idx + 1, end});\n }\n }\n ranges = next_ranges;\n parent = next_parent;\n child_type = next_child_type;\n }\n return root;\n }\n};", "memory": "30700" }
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 unordered_map<int,ListNode*>mp;\n\n TreeNode* solve(int lo,int hi){\n if(lo>hi)return NULL;\n int mid=(lo+hi)/2;\n\n TreeNode* root=new TreeNode(mp[mid]->val);\n\n root->left=solve(lo,mid-1);\n root->right=solve(mid+1,hi);\n\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n if(!head)return NULL;\n\n ListNode* temp=head;\n int cur=0;\n while(temp){\n mp[cur]=temp;\n temp=temp->next;\n cur++;\n }\n return solve(0,cur-1);\n }\n};", "memory": "30800" }
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": "class Solution {\npublic:\n TreeNode* sortedListToBST(ListNode* head) {\n if(!head) return nullptr;\n \n ListNode* dummy = new ListNode(-1);\n dummy->next = head;\n \n ListNode *slow = dummy, *fast = dummy;\n ListNode *preslow;\n \n while(fast && fast->next){\n preslow = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n \n cout << preslow->val << \", \" << slow->val << endl;\n \n \n TreeNode* root = new TreeNode(slow->val);\n \n \n preslow->next = nullptr;\n \n root->left = (slow == head) ? nullptr : sortedListToBST(head);\n \n root->right = sortedListToBST(slow->next);\n \n return root;\n }\n};\n", "memory": "31900" }
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 vector<ListNode*> split(ListNode* head){\n ListNode *p=head;\n ListNode *q=head;\n ListNode *r=NULL;\n while(q!=NULL && q->next!=NULL){\n r=p;\n p=p->next;\n q=(q->next)->next;\n }\n\n //r can never be null, since split is only called on list with length > 1\n r->next=NULL;\n return {head,p,p->next};\n }\n TreeNode* build(ListNode* head){\n if(head==NULL) return NULL;\n if(head->next==NULL) return new TreeNode(head->val);\n \n auto lists = split(head);\n TreeNode* newnode = new TreeNode(lists[1]->val);\n newnode->left=build(lists[0]);\n newnode->right=build(lists[2]);\n return newnode;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n return build(head);\n }\n};", "memory": "32000" }
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* sortedListToBST(ListNode* head) {\n vector<ListNode*> nodes;\n while(head) {\n nodes.push_back(head);\n head=head->next;\n }\n return help(nodes, 0, nodes.size());\n }\nprivate:\n TreeNode* help(vector<ListNode*>& nodes, int i, int j){\n if(i>=j) return nullptr;\n int m = (i+j)/2;\n auto root = new TreeNode(nodes[m]->val);\n delete nodes[m];\n root->left = help(nodes, i, m);\n root->right = help(nodes, m+1, j);\n return root;\n }\n};", "memory": "32100" }
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* sortedListToBST(ListNode* head) {\n vector<int> v;\n while(head!=nullptr){\n v.push_back(head->val);\n head = head->next;\n }\n function<TreeNode*(int,int)> Tree = [&](int s, int e) ->TreeNode*{\n TreeNode* root = new TreeNode;\n if(s>e)\n return nullptr;\n int mid = (s+e)/2;\n root->val = v[mid];\n root->left = Tree(s,mid-1);\n root->right = Tree(mid+1,e); \n return root;\n };\n return Tree(0,v.size()-1);\n }\n // TreeNode* Tree (int s, int e, vector<int>& v){\n // TreeNode* root = new TreeNode;\n // if(s>e){\n // return nullptr;\n // }\n // int mid = (s+e)/2;\n // root->val = v[mid];\n // root->left = Tree(s,mid-1,v);\n // root->right = Tree(mid+1,e,v); \n // return root;\n // }\n // TreeNode* sortedListToBST(ListNode* head) {\n // vector<int> v;\n // while(head!=nullptr){\n // v.push_back(head->val);\n // head = head->next;\n // }\n // return Tree(0,v.size()-1,v);\n // }\n};", "memory": "32900" }
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* sortedListToBST(ListNode* head) {\n vector<int> v;\n while(head!=nullptr){\n v.push_back(head->val);\n head = head->next;\n }\n auto Tree = [](auto &Tree, int s, int e, vector<int>& v) ->TreeNode*{\n TreeNode* root = new TreeNode;\n if(s>e)\n return nullptr;\n int mid = (s+e)/2;\n root->val = v[mid];\n root->left = Tree(Tree, s,mid-1,v);\n root->right = Tree(Tree, mid+1,e,v); \n return root;\n };\n return Tree(Tree, 0,v.size()-1,v);\n }\n // TreeNode* Tree (int s, int e, vector<int>& v){\n // TreeNode* root = new TreeNode;\n // if(s>e){\n // return nullptr;\n // }\n // int mid = (s+e)/2;\n // root->val = v[mid];\n // root->left = Tree(s,mid-1,v);\n // root->right = Tree(mid+1,e,v); \n // return root;\n // }\n // TreeNode* sortedListToBST(ListNode* head) {\n // vector<int> v;\n // while(head!=nullptr){\n // v.push_back(head->val);\n // head = head->next;\n // }\n // return Tree(0,v.size()-1,v);\n // }\n};", "memory": "32900" }
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* sortedListToBST(ListNode* head) {\n vector<int> v;\n while(head!=nullptr){\n v.push_back(head->val);\n head = head->next;\n }\n auto Tree = [&](auto& Tree, int s, int e) ->TreeNode*{\n TreeNode* root = new TreeNode;\n if(s>e)\n return nullptr;\n int mid = (s+e)/2;\n root->val = v[mid];\n root->left = Tree(Tree,s,mid-1);\n root->right = Tree(Tree,mid+1,e); \n return root;\n };\n return Tree(Tree,0,v.size()-1);\n }\n // TreeNode* Tree (int s, int e, vector<int>& v){\n // TreeNode* root = new TreeNode;\n // if(s>e){\n // return nullptr;\n // }\n // int mid = (s+e)/2;\n // root->val = v[mid];\n // root->left = Tree(s,mid-1,v);\n // root->right = Tree(mid+1,e,v); \n // return root;\n // }\n // TreeNode* sortedListToBST(ListNode* head) {\n // vector<int> v;\n // while(head!=nullptr){\n // v.push_back(head->val);\n // head = head->next;\n // }\n // return Tree(0,v.size()-1,v);\n // }\n};", "memory": "33000" }
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* sortedListToBST(ListNode* head) {\n vector<int> v;\n while(head!=nullptr){\n v.push_back(head->val);\n head = head->next;\n }\n function<TreeNode*(int,int)> Tree = [&](int s, int e) ->TreeNode*{\n TreeNode* root = new TreeNode;\n if(s>e)\n return nullptr;\n int mid = (s+e)/2;\n root->val = v[mid];\n root->left = Tree(s,mid-1);\n root->right = Tree(mid+1,e); \n return root;\n };\n return Tree(0,v.size()-1);\n }\n // TreeNode* Tree (int s, int e, vector<int>& v){\n // TreeNode* root = new TreeNode;\n // if(s>e){\n // return nullptr;\n // }\n // int mid = (s+e)/2;\n // root->val = v[mid];\n // root->left = Tree(s,mid-1,v);\n // root->right = Tree(mid+1,e,v); \n // return root;\n // }\n // TreeNode* sortedListToBST(ListNode* head) {\n // vector<int> v;\n // while(head!=nullptr){\n // v.push_back(head->val);\n // head = head->next;\n // }\n // return Tree(0,v.size()-1,v);\n // }\n};", "memory": "33100" }
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* sortedListToBST(ListNode* head) {\n vector<int> v;\n while(head!=nullptr){\n v.push_back(head->val);\n head = head->next;\n }\n auto Tree = [](auto &Tree, int s, int e, vector<int>& v) ->TreeNode*{\n TreeNode* root = new TreeNode;\n if(s>e)\n return nullptr;\n int mid = (s+e)/2;\n root->val = v[mid];\n root->left = Tree(Tree, s,mid-1,v);\n root->right = Tree(Tree, mid+1,e,v); \n return root;\n };\n return Tree(Tree, 0,v.size()-1,v);\n }\n // TreeNode* Tree (int s, int e, vector<int>& v){\n // TreeNode* root = new TreeNode;\n // if(s>e){\n // return nullptr;\n // }\n // int mid = (s+e)/2;\n // root->val = v[mid];\n // root->left = Tree(s,mid-1,v);\n // root->right = Tree(mid+1,e,v); \n // return root;\n // }\n // TreeNode* sortedListToBST(ListNode* head) {\n // vector<int> v;\n // while(head!=nullptr){\n // v.push_back(head->val);\n // head = head->next;\n // }\n // return Tree(0,v.size()-1,v);\n // }\n};", "memory": "33100" }
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* sortedListToBST(ListNode* head) {\n // vector<int> v;\n // while(head!=nullptr){\n // v.push_back(head->val);\n // head = head->next;\n // }\n // function<TreeNode*(int,int)> Tree = [&](int s, int e) ->TreeNode*{\n // TreeNode* root = new TreeNode;\n // if(s>e){\n // return nullptr;\n // }\n // else if(s==e){\n // root->val = v[s];\n // return root;\n // }\n // else{\n // int mid = (s+e)/2;\n // root->val = v[mid];\n // root->left = Tree(s,mid-1);\n // root->right = Tree(mid+1,e); \n // return root;\n // }\n // };\n // return Tree(0,v.size()-1);\n // }\n TreeNode* Tree (int s, int e, vector<int>& v){\n TreeNode* root = new TreeNode;\n if(s>e){\n return nullptr;\n }\n int mid = (s+e)/2;\n root->val = v[mid];\n root->left = Tree(s,mid-1,v);\n root->right = Tree(mid+1,e,v); \n return root;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n vector<int> v;\n while(head!=nullptr){\n v.push_back(head->val);\n head = head->next;\n }\n return Tree(0,v.size()-1,v);\n }\n};", "memory": "33200" }
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* sortedListToBST(ListNode* head) {\n vector<int> v;\n while(head!=nullptr){\n v.push_back(head->val);\n head = head->next;\n }\n auto Tree = [&](auto &Tree, int s, int e, vector<int>& v) ->TreeNode*{\n TreeNode* root = new TreeNode;\n if(s>e)\n return nullptr;\n int mid = (s+e)/2;\n root->val = v[mid];\n root->left = Tree(Tree, s,mid-1,v);\n root->right = Tree(Tree, mid+1,e,v); \n return root;\n };\n return Tree(Tree, 0,v.size()-1,v);\n }\n // TreeNode* Tree (int s, int e, vector<int>& v){\n // TreeNode* root = new TreeNode;\n // if(s>e){\n // return nullptr;\n // }\n // int mid = (s+e)/2;\n // root->val = v[mid];\n // root->left = Tree(s,mid-1,v);\n // root->right = Tree(mid+1,e,v); \n // return root;\n // }\n // TreeNode* sortedListToBST(ListNode* head) {\n // vector<int> v;\n // while(head!=nullptr){\n // v.push_back(head->val);\n // head = head->next;\n // }\n // return Tree(0,v.size()-1,v);\n // }\n};", "memory": "33200" }
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 unordered_map<int , TreeNode*>m;\n \n void rec(vector<int>&nums , int l , int h , int prevmid , bool &f)\n {\n if(l>h) return;\n int mid = (l + h) / 2;\n int present_mid = nums[mid];\n TreeNode* n = new TreeNode(present_mid);\n if(f)\n {\n m[present_mid] = n;\n if(present_mid >= prevmid)\n {\n m[prevmid]->right = n;\n }\n else m[prevmid]->left = n;\n }\n f = true;\n \n rec(nums , l , mid - 1 , present_mid , f);\n rec(nums , mid + 1 , h , present_mid , f);\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\n int l = 0 , h = nums.size() - 1;\n bool f = false;\n\n if(h == -1)\n {\n return NULL;\n }\n \n int root = nums[(l + h)/2];\n TreeNode* nn = new TreeNode(root);\n \n m[root] = nn;\n \n rec(nums , l , h , root , f);\n return m[root];\n }\n};", "memory": "33800" }
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* sortedListToBST(ListNode* head) {\n if (!head) return nullptr;\n \n vector<ListNode*> res = cutInHalf(head);\n int nodeVal = res[0]->val;\n ListNode* left = res[1];\n ListNode* right = res[2];\n\n TreeNode* root = new TreeNode(nodeVal);\n root->left = sortedListToBST(left);\n root->right = sortedListToBST(right);\n\n return root;\n }\n\n vector<ListNode*> cutInHalf(ListNode* head){\n if (head->next == nullptr) return {head, nullptr, nullptr};\n ListNode* fast = head;\n ListNode* slow = head;\n ListNode* prev = nullptr;\n\n\n while (fast && fast->next){\n fast = fast->next->next;\n prev = slow;\n slow = slow->next;\n\n }\n\n // Divide the list into two halves\n ListNode* mid = slow;\n ListNode* right = mid->next;\n mid->next = nullptr;\n\n // If the list has more than one element\n if (prev) prev->next = nullptr;\n\n return {mid, head, right};\n }\n};", "memory": "36200" }
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 vector<int> a;\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n if(nums.size()==0) return NULL;\n if(nums.size()==1) return new TreeNode(nums[0]);\n int n=nums.size()/2;\n TreeNode* x = new TreeNode(nums[n]);\n \n vector<int> leftInts(nums.begin(), nums.begin()+n);\n vector<int> rightInts(nums.begin()+n+1, nums.end());\n \n x->left = sortedArrayToBST(leftInts);\n x->right = sortedArrayToBST(rightInts);\n \n return x;\n }\n\n void makevec(ListNode* head){\n if(!head) return;\n a.push_back(head->val);\n makevec(head->next);\n return;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n makevec(head);\n return sortedArrayToBST(a);\n }\n};", "memory": "36900" }
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 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 *helper(vector<int> a)\n {\n if (a.size() == 0)\n {\n return nullptr;\n }\n int n = a.size();\n TreeNode *tree = new TreeNode(a[n / 2]);\n tree->left = helper(vector<int>(a.begin(), a.begin() + n / 2));\n tree->right = helper(vector<int>(a.begin() + n / 2 + 1, a.end()));\n return tree;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n vector<int> arr;\n ListNode *temp = head;\n while (temp)\n {\n arr.push_back(temp->val);\n temp = temp->next;\n }\n TreeNode *tree = helper(arr);\n return tree;\n }\n};", "memory": "37000" }
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* sortedArrayToBST(vector<int>& nums) {\n if(nums.size()==1){\n TreeNode* leaf = new TreeNode(nums[0]);\n return leaf;\n }\n int size = nums.size();\n TreeNode* root = new TreeNode(nums[size/2]);\n vector<int> left_tree(nums.begin(), nums.begin()+ size/2);\n vector<int> right_tree(nums.begin()+size/2+1, nums.end());\n if(left_tree.size() == 0) root->left = nullptr;\n else root->left = sortedArrayToBST(left_tree);\n if(right_tree.size() == 0) root->right = nullptr;\n else root->right = sortedArrayToBST(right_tree);\n return root;\n\n \n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n vector<int> list_nodes;\n if(head == nullptr) return nullptr;\n for(ListNode* counter= head; counter!= nullptr; counter = counter->next){\n list_nodes.push_back(counter->val);\n }\n return sortedArrayToBST(list_nodes);\n }\n};", "memory": "37100" }
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\n TreeNode* createNode(vector<int>& ListNode_vec){\n\n TreeNode* new_node = NULL;\n if (ListNode_vec.size() == 0){\n return new_node;\n }\n \n int left_idx = 0;\n int right_idx = ListNode_vec.size() - 1;\n int mid_idx = left_idx + (right_idx - left_idx)/2;\n\n // root\n new_node = new TreeNode(ListNode_vec[mid_idx]);\n // printf(\"new_node->val:%d\\n\", new_node->val);\n\n if (left_idx == right_idx){\n return new_node;\n }\n \n \n // left-side, [vec[begin], vec[mid])\n if (left_idx < mid_idx)\n {\n vector<int> left_vec(ListNode_vec.begin(), ListNode_vec.begin() + mid_idx);\n auto left_node_ptr = createNode(left_vec);\n // if(left_node_ptr)\n // printf(\"left_node_ptr->val:%d\\n\", left_node_ptr->val);\n new_node->left = left_node_ptr;\n }\n \n if (right_idx > mid_idx)\n {\n // right-side, (vec[mid], vec[end])\n vector<int> right_vec(ListNode_vec.begin() + mid_idx + 1, ListNode_vec.end());\n auto right_node_ptr = createNode(right_vec);\n // if(right_node_ptr)\n // printf(\"right_node_ptr->val:%d\\n\", right_node_ptr->val);\n new_node->right = right_node_ptr;\n }\n\n return new_node;\n \n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n // transfer node from LiskList to array\n ListNode *tmp_head = head;\n vector<int> ListNode_vec;\n while (tmp_head)\n {\n ListNode_vec.push_back(tmp_head->val);\n tmp_head = tmp_head->next;\n }\n // printf(\"ListNode_vec:\\n\");\n // for (size_t i = 0; i < ListNode_vec.size(); i++)\n // {\n // printf(\"%d,\", ListNode_vec[i]);\n // }\n // printf(\"\\n\");\n\n // \n TreeNode *root = createNode(ListNode_vec);\n \n\n return root;\n }\n};", "memory": "37200" }
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\n TreeNode* createNode(vector<int>& ListNode_vec){\n\n TreeNode* new_node = NULL;\n if (ListNode_vec.size() == 0){\n return new_node;\n }\n \n int left_idx = 0;\n int right_idx = ListNode_vec.size() - 1;\n int mid_idx = left_idx + (right_idx - left_idx)/2;\n\n // root\n new_node = new TreeNode(ListNode_vec[mid_idx]);\n // printf(\"new_node->val:%d\\n\", new_node->val);\n\n if (left_idx == right_idx){\n return new_node;\n }\n \n \n // left-side, [vec[begin], vec[mid])\n if (left_idx < mid_idx)\n {\n vector<int> left_vec(ListNode_vec.begin(), ListNode_vec.begin() + mid_idx);\n auto left_node_ptr = createNode(left_vec);\n // if(left_node_ptr)\n // printf(\"left_node_ptr->val:%d\\n\", left_node_ptr->val);\n new_node->left = left_node_ptr;\n }\n \n if (right_idx > mid_idx)\n {\n // right-side, (vec[mid], vec[end])\n vector<int> right_vec(ListNode_vec.begin() + mid_idx + 1, ListNode_vec.end());\n auto right_node_ptr = createNode(right_vec);\n // if(right_node_ptr)\n // printf(\"right_node_ptr->val:%d\\n\", right_node_ptr->val);\n new_node->right = right_node_ptr;\n }\n\n return new_node;\n \n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n // transfer node from LiskList to array\n ListNode *tmp_head = head;\n vector<int> ListNode_vec;\n while (tmp_head)\n {\n ListNode_vec.push_back(tmp_head->val);\n tmp_head = tmp_head->next;\n }\n // printf(\"ListNode_vec:\\n\");\n // for (size_t i = 0; i < ListNode_vec.size(); i++)\n // {\n // printf(\"%d,\", ListNode_vec[i]);\n // }\n // printf(\"\\n\");\n\n // \n TreeNode *root = createNode(ListNode_vec);\n \n\n return root;\n }\n};", "memory": "37300" }
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* help(vector<int> res){\n if(res.size()==0)return NULL;\n if(res.size()==1){\n return new TreeNode(res[0]);\n }\n int mid=res.size()/2;\n TreeNode* root=new TreeNode(res[mid]);\n root->left=help(vector<int>(res.begin(),res.begin()+mid));\n root->right=help(vector<int>(res.begin()+mid+1,res.end()));\n return root;\n }\npublic:\n TreeNode* sortedListToBST(ListNode* head) {\n vector<int> res;\n while(head!=NULL){\n res.push_back(head->val);\n head=head->next;\n }\n return help(res);\n }\n};", "memory": "37400" }
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 vector<int> a;\n TreeNode* sortedArrayToBST(vector<int>& nums) {\n if(nums.size()==0) return NULL;\n if(nums.size()==1) return new TreeNode(nums[0]);\n int n=nums.size()/2;\n TreeNode* x = new TreeNode(nums[n]);\n \n vector<int> leftInts(nums.begin(), nums.begin()+n);\n vector<int> rightInts(nums.begin()+n+1, nums.end());\n \n x->left = sortedArrayToBST(leftInts);\n x->right = sortedArrayToBST(rightInts);\n \n return x;\n }\n\n void makevec(ListNode* head){\n if(!head) return;\n a.push_back(head->val);\n makevec(head->next);\n head->next = NULL;\n return;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n makevec(head);\n return sortedArrayToBST(a);\n }\n};", "memory": "37500" }
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 map<string,TreeNode*> m;\n TreeNode* func(vector<TreeNode*> &v,int low , int high){\n if (low>=high) return NULL;\n string a=to_string(low)+\"-\"+to_string(high);\n if (m.find(a)!=m.end()) return m[a];\n int a1 = low+(high-low)/2;\n TreeNode* r = v[a1];\n r->left = m[to_string(low)+\"-\"+to_string(a1)] = func(v,low,a1);\n r->right =m[to_string(a1+1)+\"-\"+to_string(high)] = func(v,a1+1,high);\n return r;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n vector<TreeNode*> v;\n ListNode * present = head;\n while (present!=NULL){\n TreeNode* r = new TreeNode(present->val);\n v.push_back(r);\n present= present->next;\n }\n return func(v,0,v.size());\n }\n};", "memory": "38400" }
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 unordered_map<string,TreeNode*> m;\n TreeNode* func(vector<TreeNode*> &v,int low , int high){\n if (low>=high) return NULL;\n string a=to_string(low)+\"-\"+to_string(high);\n if (m.find(a)!=m.end()) return m[a];\n int a1 = low+(high-low)/2;\n TreeNode* r = v[a1];\n r->left = m[to_string(low)+\"-\"+to_string(a1)]=func(v,low,a1);\n r->right =m[to_string(a1+1)+\"-\"+to_string(high)] = func(v,a1+1,high);\n return r;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n vector<TreeNode*> v;\n ListNode * present = head;\n while (present!=NULL){\n TreeNode* r = new TreeNode(present->val);\n v.push_back(r);\n present= present->next;\n }\n return func(v,0,v.size());\n }\n};", "memory": "38600" }
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 unordered_map<string,TreeNode*> m;\n TreeNode* func(vector<TreeNode*> &v,int low , int high){\n if (low>=high) return NULL;\n string a=to_string(low)+\"-\"+to_string(high);\n if (m.find(a)!=m.end()) return m[a];\n int a1 = low+(high-low)/2;\n TreeNode* r = v[a1];\n r->left = m[to_string(low)+\"-\"+to_string(a1)]=func(v,low,a1);\n r->right =m[to_string(a1+1)+\"-\"+to_string(high)] = func(v,a1+1,high);\n return r;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n vector<TreeNode*> v;\n ListNode * present = head;\n while (present!=NULL){\n TreeNode* r = new TreeNode(present->val);\n v.push_back(r);\n present= present->next;\n }\n return func(v,0,v.size());\n }\n};", "memory": "38600" }
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 unordered_map<string,TreeNode*> m;\n TreeNode* func(vector<TreeNode*> &v,int low , int high){\n if (low>=high) return NULL;\n string l = to_string(high);\n string l1 = to_string(low);\n string a=l1+\"-\"+l;\n if (m.find(a)!=m.end()) return m[a];\n int a1 = low+(high-low)/2;\n TreeNode* r = v[a1];\n r->left = m[l1+\"-\"+to_string(a1)] = func(v,low,a1);\n r->right =m[to_string(a1+1)+\"-\"+l] = func(v,a1+1,high);\n return r;\n }\n TreeNode* sortedListToBST(ListNode* head) {\n vector<TreeNode*> v;\n ListNode * present = head;\n while (present!=NULL){\n TreeNode* r = new TreeNode(present->val);\n v.push_back(r);\n present= present->next;\n }\n return func(v,0,v.size());\n }\n};", "memory": "38700" }
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{\n public:\n TreeNode* sortedListToBST(ListNode* head)\n {\n vector<int> a;\n while(head!=NULL)\n {\n a.push_back(head->val);\n head=head->next;\n }\n return sortedArrayToBST(a);\n }\n TreeNode* sortedArrayToBST(vector<int>& nums) \n {\n if(nums.size()==0)\n return nullptr;\n TreeNode* root=new TreeNode;\n int a=nums.size()/2;\n root->val=nums[a];\n vector<int> l={},r={};\n int i;\n for(i=0;i<nums.size()/2;i++)\n l.push_back(nums[i]);\n for(i=nums.size()/2+1;i<nums.size();i++)\n r.push_back(nums[i]);\n root->left=sortedArrayToBST(l);\n root->right=sortedArrayToBST(r);\n return root;\n }\n};", "memory": "40300" }
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\n TreeNode* f(vector <int> v){\n if(v.size() == 0) return nullptr;\n TreeNode *root = new TreeNode(v[v.size()/2]);\n\n if(v.size() == 1) return root;\n if(v.size() == 2) {\n TreeNode *r = new TreeNode(v[0]);\n root->left = r; return root;\n }\n \n vector <int> l(v.begin(), v.begin() + v.size()/2);\n root->left = f(l);\n vector <int> r(v.begin() + v.size()/2 + 1, v.end());\n root->right = f(r);\n\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n vector <int> v;\n ListNode *temp = head;\n while(temp){\n v.push_back(temp->val);\n temp = temp->next;\n }\n\n if(v.size() == 0) return nullptr;\n TreeNode *root = new TreeNode(v[v.size()/2]);\n\n if(v.size() == 1) return root;\n if(v.size() == 2) {\n TreeNode *r = new TreeNode(v[0]);\n root->left = r; return root;\n }\n \n vector <int> l(v.begin(), v.begin() + v.size()/2);\n root->left = f(l);\n vector <int> r(v.begin() + v.size()/2 + 1, v.end());\n root->right = f(r);\n\n return root;\n }\n};", "memory": "41900" }
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\n TreeNode* f(vector <int> v){\n if(v.size() == 0) return nullptr;\n TreeNode *root = new TreeNode(v[v.size()/2]);\n\n if(v.size() == 1) return root;\n if(v.size() == 2) {\n TreeNode *r = new TreeNode(v[0]);\n root->left = r; return root;\n }\n \n vector <int> l(v.begin(), v.begin() + v.size()/2);\n root->left = f(l);\n vector <int> r(v.begin() + v.size()/2 + 1, v.end());\n root->right = f(r);\n\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n vector <int> v;\n ListNode *temp = head;\n while(temp){\n v.push_back(temp->val);\n temp = temp->next;\n }\n\n if(v.size() == 0) return nullptr;\n TreeNode *root = new TreeNode(v[v.size()/2]);\n\n if(v.size() == 1) return root;\n if(v.size() == 2) {\n TreeNode *r = new TreeNode(v[0]);\n root->left = r; return root;\n }\n \n vector <int> l(v.begin(), v.begin() + v.size()/2);\n root->left = f(l);\n vector <int> r(v.begin() + v.size()/2 + 1, v.end());\n root->right = f(r);\n\n return root;\n }\n};", "memory": "42000" }
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\n TreeNode* f(vector <int> v){\n if(v.size() == 0) return nullptr;\n TreeNode *root = new TreeNode(v[v.size()/2]);\n\n if(v.size() == 1) return root;\n if(v.size() == 2) {\n TreeNode *r = new TreeNode(v[0]);\n root->left = r; return root;\n }\n \n vector <int> l(v.begin(), v.begin() + v.size()/2);\n root->left = f(l);\n vector <int> r(v.begin() + v.size()/2 + 1, v.end());\n root->right = f(r);\n\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n vector <int> v;\n ListNode *temp = head;\n while(temp){\n v.push_back(temp->val);\n temp = temp->next;\n }\n\n if(v.size() == 0) return nullptr;\n TreeNode *root = new TreeNode(v[v.size()/2]);\n\n if(v.size() == 1) return root;\n if(v.size() == 2) {\n TreeNode *r = new TreeNode(v[0]);\n root->left = r; return root;\n }\n \n vector <int> l(v.begin(), v.begin() + v.size()/2);\n root->left = f(l);\n vector <int> r(v.begin() + v.size()/2 + 1, v.end());\n root->right = f(r);\n\n return root;\n }\n};", "memory": "42100" }
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\n TreeNode* f(vector <int> v){\n if(v.size() == 0) return nullptr;\n TreeNode *root = new TreeNode(v[v.size()/2]);\n\n if(v.size() == 1) return root;\n if(v.size() == 2) {\n TreeNode *r = new TreeNode(v[0]);\n root->left = r; return root;\n }\n \n vector <int> l(v.begin(), v.begin() + v.size()/2);\n root->left = f(l);\n vector <int> r(v.begin() + v.size()/2 + 1, v.end());\n root->right = f(r);\n\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n vector <int> v;\n ListNode *temp = head;\n while(temp){\n v.push_back(temp->val);\n temp = temp->next;\n }\n\n if(v.size() == 0) return nullptr;\n TreeNode *root = new TreeNode(v[v.size()/2]);\n\n if(v.size() == 1) return root;\n if(v.size() == 2) {\n TreeNode *r = new TreeNode(v[0]);\n root->left = r; return root;\n }\n \n vector <int> l(v.begin(), v.begin() + v.size()/2);\n root->left = f(l);\n vector <int> r(v.begin() + v.size()/2 + 1, v.end());\n root->right = f(r);\n\n return root;\n }\n};", "memory": "42100" }
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* createBST(TreeNode* root, vector<int> vec) {\n if(vec.empty()) {\n return root;\n }\n else if(vec.size() == 1) {\n return new TreeNode(vec[0]);\n }\n int midd = vec.size() / 2;\n root = new TreeNode(vec[midd]);\n vector<int> leftist(vec.begin(), vec.begin() + midd);\n vector<int> rightist(vec.begin() + midd + 1, vec.end());\n root->left = createBST(root->left, leftist);\n root->right = createBST(root->right, rightist);\n return root;\n }\n\n TreeNode* sortedListToBST(ListNode *head) {\n vector<int> vec;\n ListNode* temp = head;\n while(temp != NULL) {\n vec.push_back(temp->val);\n temp = temp->next;\n }\n TreeNode* root = NULL;\n if(vec.empty()) {\n return root;\n }\n root = createBST(root, vec);\n return root;\n }\n};", "memory": "42700" }
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 \n TreeNode* create(TreeNode* root,vector<int> arr) {\n if(arr.size()==0) return NULL;\n int mid=arr.size()/2;\n root=new TreeNode(arr[mid]);\n vector<int> leftpart(arr.begin(),arr.begin()+mid);\n vector<int> rightpart(arr.begin()+mid+1,arr.end());\n \n root->left=create(root,leftpart);\n root->right=create(root,rightpart);\n\n return root;\n\n }\n TreeNode* sortedListToBST(ListNode* head) {\n if(head==NULL) return NULL; \n vector<int> arr;\n ListNode* temp=head;\n while(temp!=NULL) {\n arr.push_back(temp->val);\n temp=temp->next;\n }\n TreeNode* root=NULL;\n\n root=create(root,arr);\n return root;\n }\n};", "memory": "42800" }
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 */\nTreeNode* fun(vector<int> l) {\n if (l.size() == 0) {\n return nullptr;\n }\n\n int mid = (0 + l.size()) / 2;\n TreeNode* node = new TreeNode(l[mid]);\n\n vector<int> left;\n vector<int> right;\n\n for (int i = 0; i < l.size(); i++) {\n if (i == mid) {\n continue;\n }\n if (i < mid) {\n left.push_back(l[i]);\n } else {\n right.push_back(l[i]);\n }\n }\n\n node->left = fun(left);\n node->right = fun(right);\n\n return node;\n\n\n\n\n}\nclass Solution {\npublic:\n TreeNode* sortedListToBST(ListNode* head) {\n \n vector<int> vec;\n ListNode* curr = head;\n while (curr != nullptr) {\n vec.push_back(curr->val);\n curr = curr->next;\n }\n\n return fun(vec);\n\n\n }\n};", "memory": "43900" }
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* recursiveListToBST(vector<ListNode*> tree, int left, int right){\n if (left > right) return NULL; // since right = mid-1 and left = mid+1 may go out of bounds\n if (right == left){ // base case of when only 1 node in list\n return new TreeNode(tree[left]->val);\n }\n int mid = left + (right-left)/2;\n TreeNode* parent = new TreeNode(tree[mid]->val);\n parent->left = recursiveListToBST(tree, left, mid-1);\n parent->right = recursiveListToBST(tree, mid+1, right);\n return parent;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n if (!head) return NULL;\n vector<ListNode*> tree{};\n while (head){\n tree.push_back(head);\n head = head->next;\n }\n return recursiveListToBST(tree, 0, tree.size()-1);\n }\n};", "memory": "342500" }
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* sortedListToBST(ListNode* head) {\n int n = 0;\n ListNode *cur = head;\n while(cur)\n {\n n++;\n cur = cur->next;\n }\n\n vector<ListNode *>arr(n);\n cur = head;\n int idx = 0;\n while(cur)\n {\n arr[idx] = cur;\n cur = cur->next;\n idx++;\n }\n\n return cBST(arr, 0, n-1);\n }\n\n TreeNode* cBST(vector<ListNode *>arr, int left, int right)\n {\n\n if (left > right)\n return NULL;\n \n int idx = (left + right )/2;\n\n TreeNode *node = new TreeNode(arr[idx]->val);\n node->left = cBST(arr, left, idx-1);\n node->right = cBST(arr, idx+1, right);\n return node;\n }\n};", "memory": "348500" }
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* recursiveListToBST(vector<ListNode*> tree, int left, int right){\n if (left > right) return NULL; // since right = mid-1 and left = mid+1 may go out of bounds\n int mid = left + (right-left)/2;\n TreeNode* parent = new TreeNode(tree[mid]->val);\n parent->left = recursiveListToBST(tree, left, mid-1);\n parent->right = recursiveListToBST(tree, mid+1, right);\n return parent;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n if (!head) return NULL;\n vector<ListNode*> tree{};\n while (head){\n tree.push_back(head);\n head = head->next;\n }\n return recursiveListToBST(tree, 0, tree.size()-1);\n }\n};", "memory": "348800" }
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* CreateBST(vector<ListNode*> arr,int s,int e){\n if(s>e){\n return NULL;\n } \n ListNode* mid = arr[(s+e)/2]; \n int index = (s+e)/2;\n TreeNode* root = new TreeNode(mid->val);\n\n root->left = CreateBST(arr,s,index-1);\n root->right= CreateBST(arr,index+1,e); \n\n return root;\n \n }\n TreeNode* sortedListToBST(ListNode* head) {\n int size = 0;\n vector<ListNode*> arr;\n ListNode* temp = head;\n while(temp){\n size++;\n arr.push_back(temp);\n temp =temp->next;\n }\n return CreateBST(arr,0,size-1);\n \n }\n};", "memory": "348900" }
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* helper(int l, int r, vector<ListNode*> qa){\n if(l >= r) return NULL;\n\n int mid = (l+r)/2;\n TreeNode* head = new TreeNode(qa[mid]->val);\n head->left = helper(l, mid, qa);\n head->right = helper(mid+1, r, qa);\n return head;\n }\n\n TreeNode* sortedListToBST(ListNode* head) {\n if(head == NULL) return NULL;\n\n vector<ListNode*> qa;\n ListNode* curr = head;\n while(curr){\n qa.push_back(curr);\n curr = curr->next;\n }\n\n int n = qa.size();\n\n return helper(0, n, qa);\n }\n};", "memory": "349100" }
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\n void insertIntoTree(TreeNode* tree, vector<int> vals, int leftIdx, int rightIdx, bool isLeft)\n {\n if (tree == 0) return ;\n int mid = (leftIdx + rightIdx) / 2;\n \n TreeNode* temp = new TreeNode(vals[mid]);\n if(isLeft)\n {\n tree->left = temp;\n }\n else\n {\n tree->right = temp;\n }\n \n if(leftIdx == rightIdx)\n {\n return;\n }\n\n if(leftIdx < mid)\n {\n insertIntoTree(temp, vals, leftIdx, mid - 1, true);\n }\n\n if(mid < rightIdx)\n {\n insertIntoTree(temp, vals, mid + 1, rightIdx, false);\n } \n }\n\n TreeNode* sortedListToBST(ListNode* head) \n {\n if (head == 0) return 0;\n vector<int> vals;\n ListNode* temp = head;\n\n while(temp!= 0)\n {\n vals.push_back(temp->val);\n temp = temp->next;\n }\n\n int mid = vals.size()/2;\n TreeNode* root = new TreeNode(vals[mid]);\n \n if (mid > 0)\n {\n insertIntoTree(root, vals, 0, mid - 1, true);\n }\n\n if (mid + 1 < vals.size())\n {\n insertIntoTree(root, vals, mid + 1, vals.size() - 1, false);\n }\n return root;\n }\n};", "memory": "354400" }
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 void sortedListToBSTHelper(vector<int> array, TreeNode* root, int left, int mid, int right) {\n if(left < 0 || right > array.size()-1 || left > right || root == NULL)\n return;\n\n root->val = array[mid];\n if(left >= 0 && mid-1 <= array.size()-1 && left <= mid-1 && root != NULL) {\n root->left = new TreeNode(array[(left+mid-1)/2]);\n sortedListToBSTHelper(array, root->left, left, (left+mid-1)/2, mid-1);\n }\n if(mid+1 >= 0 && right <= array.size()-1 && mid+1 <= right && root != NULL) {\n root->right = new TreeNode(array[(right+mid+1)/2]);\n sortedListToBSTHelper(array, root->right, mid+1, (right+mid+1)/2, right);\n } \n }\n TreeNode* sortedListToBST(ListNode* head) {\n if(head == NULL) {\n return NULL;\n }\n //convert list to array\n vector<int> array;\n ListNode* curr = head;\n while(curr != NULL) {\n array.push_back(curr->val);\n curr=curr->next;\n }\n // cout << array[0];\n // return NULL;\n TreeNode* root = new TreeNode(array[(0 + array.size()-1)/2]);\n // cout << root->val;\n // return NULL;\n sortedListToBSTHelper(array, root, 0, (0 + array.size()-1)/2, array.size()-1);\n return root; \n }\n};\n\n/*\n 0. 1. 2 3 4\n-10 -3 0 5 9\n \n 0 2 4\n\n\n\n\n*/", "memory": "354500" }
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* sortedListToBST(ListNode* head) {\n vector<int> arr;\n TreeNode* tree=new TreeNode();\n if(head==nullptr){return nullptr;}\n while(head!=nullptr){arr.push_back(head->val);head=head->next;}\n helper(arr,tree,0,arr.size());\n return tree;\n \n }\n void helper(vector<int> arr,TreeNode* root,int low,int high){\n\n if(arr.size()==0){return;}\n int mid=(low+high)/2;\n root->val=arr[mid];\n if(low!=mid){\n TreeNode* left=new TreeNode();\n root->left=left;\n helper(arr,left,low,mid);\n }\n if(mid!=high-1){\n\n TreeNode* right=new TreeNode();\n root->right=right;\n helper(arr,right,mid+1,high);\n }\n \n }\n};", "memory": "354700" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n pair<bool,int> check(TreeNode* root){\n if(root == NULL){\n pair<bool, int> p = make_pair(true,0);\n return p;\n }\n\n pair<bool, int> left = check(root->left);\n pair<bool,int> right = check(root->right);\n\n bool leftAns = left.first;\n bool rightAns = right.first;\n\n bool diff = abs(left.second - right.second) <=1;\n\n pair<bool,int> ans;\n ans.second= max(left.second , right.second) +1;\n\n if(leftAns && rightAns && diff){\n ans.first = true;\n }else{\n ans.first = false;\n }\n return ans;\n }\npublic:\n bool isBalanced(TreeNode* root) {\n bool ans = check(root).first;\n if(root != NULL){\n root->left = NULL;\n root->right = NULL;\n }\n return ans;\n }\n};", "memory": "18000" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 {\n int traverse(TreeNode* node) {\n if (!node) return 0;\n int left = traverse(node->left);\n node->left = NULL;\n if (left == -1) return -1;\n int right = traverse(node->right);\n node->right = NULL;\n if (right == -1) return -1;\n if (left - right > 1 || right - left > 1) return -1;\n return max(left, right) + 1;\n } \npublic:\n bool isBalanced(TreeNode* root) {\n return traverse(root) >= 0; \n }\n};", "memory": "20900" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 {\n int traverse(TreeNode* node) {\n if (!node) return 0;\n int left = traverse(node->left);\n node->left = NULL;\n if (left == -1) return -1;\n int right = traverse(node->right);\n node->right = NULL;\n if (right == -1) return -1;\n if (left - right > 1 || right - left > 1) return -1;\n return max(left, right) + 1;\n } \npublic:\n bool isBalanced(TreeNode* root) {\n int result = traverse(root);\n \n return result >= 0;\n }\n};", "memory": "21100" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 {\nprivate:\n int height(TreeNode* root){\n if(!root) return -1;\n \n return max(height(root->left), height(root->right)) + 1;\n }\npublic:\n bool isBalanced(TreeNode* root) {\n if(!root) return true;\n \n return abs(height(root->left) - height(root->right)) < 2 and isBalanced(root->left) and isBalanced(root->right);\n }\n};", "memory": "21500" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 bool isBalanced(TreeNode* root) {\n return dfsheight(root) != -1;\n }\n\n int dfsheight(TreeNode* root){\n if(root == NULL)return 0;\n\n int leftheight = dfsheight(root -> left);\n if(leftheight == -1)return -1;\n int rightheight = dfsheight(root->right);\n if(rightheight == -1)return -1;\n\n if(abs(leftheight-rightheight)>1)return -1;\n return max(leftheight,rightheight)+1;\n }\n};", "memory": "21500" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 height(TreeNode* root){\n if(!root)\n return 0;\n else{\n int LH=height(root->left);\n int RH=height(root->right);\n return 1+max(LH,RH);\n }\n }\n bool isBalanced(TreeNode* root) {\n if(!root)\n return true;\n int LH=height(root->left);\n int RH=height(root->right);\n if(abs(LH-RH)<=1 && isBalanced(root->left) && isBalanced(root->right))\n return true;\n return false;\n\n \n }\n};", "memory": "21600" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 solve(TreeNode* root, bool& ans) {\n if (root == NULL)\n return 0;\n int left = solve(root->left, ans);\n int right =solve(root->right, ans);\n if(abs(left - right)>1)ans=false;\n return max(left + 1, right + 1);\n }\n bool isBalanced(TreeNode* root) \n {\n bool ans= true;\n solve(root, ans);\n return ans;\n }\n};", "memory": "21600" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 check(TreeNode* root){\n \n if(root==NULL) return 0;\n \n int left = check(root->left);\n int right = check(root->right);\n if(left==-1 || right==-1) return -1;\n if(abs(left-right)>1) return -1;\n return max(left,right)+1;\n\n }\n\n int bst(TreeNode* root , int &height){\n if(root == NULL) return 0;\n\n int left = 1+bst(root->left,height);\n int right = 1+bst(root->right,height);\n if(abs(left-right)>1) height = 0;\n return max(left,right); \n }\n bool isBalanced(TreeNode* root) {\n if(root==NULL) return true;\n //return (check(root)==-1?false:true);\n int height =1;\n bst(root,height);\n if(height==0) return false;\n else return true;\n }\n};", "memory": "21700" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 bool isBalanced(TreeNode* root) {\n return Height (root) != -1;\n }\n int Height (TreeNode* root){\n if(root == NULL) return 0;\n\n int leftHeight = Height(root -> left);\n int rightHeight = Height(root -> right);\n\n if(leftHeight == -1 || rightHeight == -1) return -1;\n if(abs(leftHeight - rightHeight) > 1) return -1;\n\n return max(leftHeight, rightHeight) + 1;\n }\n};", "memory": "21700" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 getheight(TreeNode* root){\n if(root == nullptr){\n return 0;\n }\n int lh= getheight(root->left);\n int rh= getheight(root->right);\n\n return 1+ max(lh,rh);\n }\n bool isBalanced(TreeNode* root) {\n if(root == nullptr){\n return true;\n }\n int left = getheight(root->left);\n int right = getheight(root->right);\n\n if(abs(left-right)<=1 && isBalanced(root->left) && isBalanced(root->right)){\n return true;\n }\n\n return false;\n }\n};", "memory": "21800" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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\n\n /*\nclass Solution {\n int height(TreeNode* root){\n //base case\n if(root == NULL) {\n return 0;\n }\n \n int left = height(root ->left);\n int right = height(root->right);\n \n int ans = max(left, right) + 1;\n return ans;\n \n }\npublic:\n bool isBalanced(TreeNode* root) {\n if(root==NULL){\n return true;\n }\n\n bool left=isBalanced(root->left);\n bool right=isBalanced(root->right);\n bool difference=abs(height(root->left)- height(root->right))<=1;\n if(left && right&& difference){\n return true;\n }\n else{\n return false;\n }\n \n\n }\n};\n*/\n\nclass Solution {\n pair<bool, int>isbalancedFast(TreeNode* root){\n\n if(root==NULL){\n pair<bool, int> p= make_pair(true, 0);\n return p;\n }\n pair<int, int> left= isbalancedFast(root->left);\n pair<int, int> right= isbalancedFast(root->right);\n bool leftans=left.first;\n bool rightans= right.first;\n \n bool difference=abs(left.second - right.second)<=1;\n\n pair<bool,int> ans;\n ans.second=max(left.second, right.second)+1;\n if(leftans && rightans && difference){\n ans.first=true;\n }\n else{\n ans.first= false;\n }\n\nreturn ans;\n }\npublic:\n bool isBalanced(TreeNode* root) {\n return isbalancedFast(root).first;\n }\n};", "memory": "21800" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 int height(TreeNode* root){\n if(!root)\n return 0;\n\n int left = height(root->left);\n int right = height(root->right);\n return max(left,right)+1;\n }\n bool isBalanced(TreeNode* root) {\n if(!root)\n return true;\n if(root->left==NULL && root->right==NULL)\n return true;\n\n return (\n abs(height(root->right)-height(root->left))<=1\n &&\n isBalanced(root->left)\n &&\n isBalanced(root->right)\n );\n }\n};", "memory": "21900" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 bool isBalanced(TreeNode* root) {\n return height(root) != -1;\n }\n int height(TreeNode* root) {\n if (root) {\n int left = height(root->left);\n int right = height(root->right);\n\n if (left < 0 || right < 0 | abs(left - right) > 1) return -1;\n return 1+max(left, right); \n } else {\n return 0;\n }\n }\n};", "memory": "21900" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int height(TreeNode* root)\n {\n // base case\n if(root == NULL) return 0;\n\n int leftH = height(root->left);\n int rightH = height(root->right);\n int ans = max(leftH,rightH) + 1;\n return ans;\n }\n bool balance(TreeNode* root)\n {\n // base case\n if(root == NULL) return true;\n\n int leftH = height(root->left);\n int rightH = height(root->right);\n\n int diff = abs(leftH - rightH);\n \n bool leftBal = balance(root->left);\n bool rightBal = balance(root->right);\n\n if((leftBal) && (rightBal) && (diff <= 1))\n return true;\n\n return false;\n }\n bool isBalanced(TreeNode* root) {\n if(root == NULL) return true;\n\n return balance(root);\n }\n};", "memory": "22000" }
110
<p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> true </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></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 int height(TreeNode* root)\n {\n if(root==NULL)\n {\n return 0;\n }\n\n int lh = height(root->left);\n int rh = height(root->right);\n\n return max(lh, rh) + 1;\n }\n\n\n bool isBalanced(TreeNode* root) {\n if(root==NULL)\n {\n return true;\n }\n\n bool lb = isBalanced(root->left);\n bool rb = isBalanced(root->right);\n\n int lh = height(root->left);\n int rh = height(root->right);\n\n if(abs(lh-rh)>1)\n {\n return false;\n }\n\n \n return (lb&rb);\n }\n};", "memory": "22000" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int speed = []() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n return 0;\n }();\n int minDepth(TreeNode* root) {\n if (!root)\n return 0;\n\n int left = minDepth(root->left);\n int right = minDepth(root->right);\n\n if (right == 0 || left == 0)\n return max (left, right) + 1;\n root->left = nullptr;\n root->right = nullptr;\n return min(left, right) + 1;\n }\n};", "memory": "101600" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "#include <algorithm>\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 */\n\n#pragma GCC optimize(\"Ofast,unroll-loops\")\n#pragma GCC target(\"avx2,tune=native\")\nstatic int speed = []() {\nstd::ios::sync_with_stdio(false);\nstd::cin.tie(nullptr);\nreturn 0;\n}();\n\nclass Solution {\npublic:\n int minDepth(TreeNode* root) {\n if (root == NULL) return 0;\n if (root->right == NULL && root->left == NULL) return 1;\n short ld=SHRT_MAX;\n short rd=SHRT_MAX;\n if (root->left) ld = minDepth(root->left);\n if (root->right) rd = minDepth(root->right);\n // reset memory\n root->right = nullptr;\n root->left = nullptr;\n return std::min(ld,rd)+1;\n }\n};", "memory": "101700" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 minDepth(TreeNode* root) {\n if (!root)\n return 0;\n\n int left = minDepth(root->left);\n int right = minDepth(root->right);\n\n if (right == 0 || left == 0)\n return max (left, right) + 1;\n root->left = nullptr;\n root->right = nullptr;\n return min(left, right) + 1;\n }\n};", "memory": "101800" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 minDepth(TreeNode* root) {\n if(root==NULL){\n return 0;\n }\n int lh=minDepth(root->left);\n int rh=minDepth(root->right);\n if (rh == 0 || lh == 0)\n return max (lh, rh) + 1;\n root->left = NULL;\n root->right = NULL;\n return min(lh, rh) + 1;\n }\n};", "memory": "101900" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 minDepth(TreeNode* root) {\n if (!root)\n return 0;\n\n int left = minDepth(root->left);\n int right = minDepth(root->right);\n\n if (right == 0 || left == 0)\n return max (left, right) + 1;\n root->left = nullptr;\n root->right = nullptr;\n return min(left, right) + 1;\n }\n};", "memory": "102000" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 minDepth(TreeNode* root) {\n if(root == NULL) return 0;\n\n int left = minDepth(root->left);\n int right = minDepth(root->right);\n\n if(right == 0 || left == 0) //meaning leaf node\n return max(left, right) + 1;\n root->left = NULL;\n root->right = NULL;\n \n return min(left, right) +1;\n }\n};", "memory": "102100" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 speed = []() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n return 0;\n }();\n\n int minDepth(TreeNode* root) {\n if (root == NULL) return 0;\n if ( root->left == NULL && root->right == NULL ) return 1;\n\n int left = minDepth(root->left);\n int right = minDepth(root->right);\n root->left = NULL;\n root->right == NULL;\n\n if (!left || !right) return max(left, right) + 1;\n return min(left, right) + 1;\n }\n};", "memory": "102200" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 ans = INT_MAX;\n void solve(TreeNode* root, int depth){\n if(!root) return;\n if(!root -> right && !root -> left) ans = min(ans, depth);\n solve(root -> left, depth + 1);\n solve(root -> right, depth + 1);\n }\n\n int minDepth(TreeNode* root) {\n if(!root) return 0;\n solve(root, 1);\n return ans;\n }\n};\n\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "144500" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 min_num=INT_MAX;\n void leafDepth(TreeNode* root, int num){\n if(root!=NULL){\n if(root->left==NULL && root->right == NULL && num<min_num){\n min_num= num;\n }\n leafDepth(root->left,num+1);\n leafDepth(root->right,num+1);\n\n }\n\n }\n int minDepth(TreeNode* root) {\n if(root==NULL){\n return 0;\n }\n leafDepth(root,1);\n return min_num;\n \n \n }\n};", "memory": "144600" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 // ignore this part\n int speed = []() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n return 0;\n }();\n \n int minDepth(TreeNode* root) {\n // no node left\n if(root==nullptr) return 0;\n \n // single node left (current)\n else if(root->left==nullptr && root->right==nullptr) \n return 1;\n \n // only right node is null\n else if(root->left!=nullptr && root->right==nullptr) \n return minDepth(root->left)+1;\n \n // only left node is null \n else if(root->right!=nullptr && root->left==nullptr)\n return minDepth(root->right)+1;\n\n // no node is null\n else\n return min(minDepth(root->left), minDepth(root->right))+1;\n }\n};", "memory": "144700" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 solve(TreeNode* root, int& ans, int depth) {\n if (root == nullptr) return;\n depth++;\n if (root->left == nullptr && root->right == nullptr) {\n ans = min(ans, depth);\n return;\n }\n solve(root->left, ans, depth);\n solve(root->right, ans, depth);\n }\n int minDepth(TreeNode* root) {\n int ans = INT_MAX;\n solve(root, ans, 0);\n if (ans == INT_MAX) ans = 0;\n return ans;\n }\n};", "memory": "144800" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 minDepth(TreeNode* root) {\n if (root==NULL){\n return 0;\n }\n if (root->left == NULL && root->right == NULL){\n return 1;\n }\n if (root->left==NULL && root->right != NULL){\n return minDepth(root->right)+1;\n }\n if (root->left!=NULL && root->right==NULL){\n return minDepth(root->left)+1;\n }\n return min(minDepth(root->left), minDepth(root->right))+1;\n }\n};", "memory": "144800" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 minDepth(TreeNode* root) {\n if(root==NULL)\n return 0;\n if(root->left==NULL && root->right==NULL)\n return 1;\n int lh=INT_MAX,rh=INT_MAX;\n if(root->left)\n lh= minDepth(root->left);\n if(root->right) \n rh= minDepth(root->right);\n //if(root->left && root->right)\n return 1+min(lh,rh);;\n \n //return 1+max(lh,rh);\n }\n};", "memory": "144900" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 minDepth(TreeNode* root) {\n if(!root) return 0;\n int l = minDepth(root->left);\n int r = minDepth(root->right);\n if(l==0 && r==0) return 1;\n if((l!=0 && l<r)||r==0) return l+1;\n else return r+1;\n }\n};", "memory": "144900" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n int minDepth(TreeNode* root) {\n if(root == NULL) return 0;\n int count = 1;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int n = q.size();\n for(int i=0; i<n; i++){\n TreeNode* curr = q.front();\n q.pop();\n if(curr->left==NULL && curr->right==NULL) return count;\n if(curr->left){\n q.push(curr->left);\n }\n if(curr->right){\n q.push(curr->right);\n }\n }\n count++;\n }\n\n return count;\n }\n};", "memory": "145000" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minDepth(TreeNode* root) {\n if(!root) return 0;\n if(!root->left && !root->right)\n return 1;\n queue<TreeNode*> que;\n que.push(root);\n int depth = 1;\n while(!que.empty()) {\n int n = que.size();\n while(n--) {\n TreeNode* temp = que.front();\n que.pop();\n if(!temp->left && !temp->right)\n return depth;\n if(temp->left)\n que.push(temp->left);\n if(temp->right)\n que.push(temp->right);\n }\n depth++; \n }\n return -1;\n }\n};", "memory": "145000" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 int minDepth(TreeNode* root) {\n if(root == NULL)\n {\n return 0;\n }\n else if(root -> left == NULL && root -> right == NULL)\n {\n return 1;\n }\n else if(root -> left != NULL && root -> right == NULL)\n {\n return minDepth(root -> left) + 1;\n }\n else if(root -> left == NULL && root -> right != NULL)\n {\n return minDepth(root -> right) + 1;\n }\n \n else\n {\n return min(minDepth(root -> left), minDepth(root -> right)) + 1;\n }\n }\n};\n", "memory": "145100" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 int minDepth(TreeNode* root) {\n if(!root)\n return 0;\n \n queue<TreeNode*> q;\n q.push(root);\n int depth=1;\n\n while(!q.empty())\n {\n int cnt=q.size();\n for (int i=0;i<cnt;i++)\n {\n TreeNode* node=q.front();\n q.pop();\n\n if(node->left==NULL && node->right==NULL )\n return depth;\n if(node->left)\n q.push(node->left);\n if(node->right)\n q.push(node->right);\n \n }\n depth++;\n }\n return depth;\n }\n};", "memory": "145100" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minDepth(TreeNode* root) {\n if (root == nullptr) return 0;\n\n std::queue<TreeNode*> queue;\n queue.push(root);\n int level = 1;\n\n while (!queue.empty()) {\n int levelSize = queue.size();\n\n for (int i = 0; i < levelSize; i++) {\n TreeNode* node = queue.front();\n queue.pop();\n\n if (node->left == nullptr && node->right == nullptr) {\n return level;\n }\n\n if (node->left != nullptr) {\n queue.push(node->left);\n }\n\n if (node->right != nullptr) {\n queue.push(node->right);\n }\n }\n\n level++;\n }\n\n return level;\n }\n};", "memory": "145200" }
111
<p>Given a binary tree, find its minimum depth.</p> <p>The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.</p> <p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg" style="width: 432px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 2 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [2,null,3,null,4,null,5,null,6] <strong>Output:</strong> 5 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</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 int minDepth(TreeNode* root) {\n if(!root)\n return 0;\n if(!root->left && !root->right){\n return 1;\n }\n int l=minDepth(root->left);\n int r=minDepth(root->right);\n if(!root->left)\n return r+1;\n if(!root->right)\n return l+1;\n return min(l,r)+1;\n }\n};", "memory": "145200" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool hasPathSum(TreeNode* root, int targetSum) {\n if (!root) {\n return false;\n }\n\n return helper(root, targetSum);\n }\n\n bool helper(TreeNode* root, int targetSum) {\n if (!root) {\n return false;\n }\n\n targetSum -= root->val;\n\n if (!root->left && !root->right) {\n return targetSum == 0;\n }\n\n bool left = helper(root->left, targetSum);\n bool right = helper(root->right, targetSum);\n root->left = nullptr;\n root->right = nullptr;\n\n return left || right;\n }\n};", "memory": "14200" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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),\n * right(right) {}\n * };\n */\nclass Solution {\n static bool init;\n public:\n Solution() {\n if (!init) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n init = true;\n }\n }\n\npublic:\n bool hasPathSum(TreeNode* root, int targetSum) {\n if (!root) {\n return false;\n }\n\n return helper(root, targetSum);\n }\n\n bool helper(TreeNode* root, int targetSum) {\n if (!root) {\n return false;\n }\n\n targetSum -= root->val;\n\n if (!root->left && !root->right) {\n return targetSum == 0;\n }\n\n bool left = helper(root->left, targetSum);\n bool right = helper(root->right, targetSum);\n root->left = nullptr;\n root->right = nullptr;\n\n return left || right;\n }\n};\n\nbool Solution::init = false;", "memory": "14300" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n if (!root) return false;\n targetSum -= root->val;\n if (!root->left && !root->right) return targetSum == 0;\n bool left = hasPathSum(root->left, targetSum), right = hasPathSum(root->right, targetSum);\n root->left = NULL;\n root->right = NULL;\n return left || right;\n }\n};", "memory": "14400" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n if (!root) return false;\n targetSum -= root->val;\n if (!root->left && !root->right) return targetSum == 0;\n bool left = hasPathSum(root->left, targetSum), right = hasPathSum(root->right, targetSum);\n root->left = NULL;\n root->right = NULL;\n return left || right;\n }\n};", "memory": "14400" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n\n if(root == NULL) return false;\n else if(root->left==NULL && root->right==NULL && targetSum-root->val == 0) return true;\n else\n {\n bool left = hasPathSum(root->left, targetSum-root->val);\n bool right = hasPathSum(root->right, targetSum-root->val);\n root->left = NULL;\n root->right = NULL;\n return left || right;\n }\n\n }\n};", "memory": "14500" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "#include <vector>\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 bool hasPathSum(TreeNode* root, int targetSum) {\n if (not root) {\n return false;\n }\n \n vector<TreeNode*> visitStack = {root};\n int currentSum = 0;\n bool popped = false;\n while (not visitStack.empty()) {\n if (not popped) {\n currentSum += visitStack.back()->val;\n }\n \n if (visitStack.back()->left) {\n TreeNode *temp = visitStack.back()->left;\n visitStack.back()->left = nullptr;\n visitStack.push_back(temp);\n popped = false;\n }\n else if (visitStack.back()->right) {\n TreeNode *temp = visitStack.back()->right;\n visitStack.back()->right = nullptr;\n visitStack.push_back(temp);\n popped = false;\n }\n else {\n if (not popped and currentSum == targetSum) {\n return true;\n }\n else {\n currentSum -= visitStack.back()->val;\n visitStack.pop_back();\n popped = true;\n }\n }\n }\n return false;\n }\n};", "memory": "14600" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "#include <vector>\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 bool hasPathSum(TreeNode* root, int targetSum) {\n if (not root) {\n return false;\n }\n \n vector<TreeNode*> visitStack = {root};\n int currentSum = 0;\n bool popped = false;\n while (not visitStack.empty()) {\n if (not popped) {\n currentSum += visitStack.back()->val;\n }\n \n if (visitStack.back()->left) {\n TreeNode *temp = visitStack.back()->left;\n visitStack.back()->left = nullptr;\n visitStack.push_back(temp);\n popped = false;\n }\n else if (visitStack.back()->right) {\n TreeNode *temp = visitStack.back()->right;\n visitStack.back()->right = nullptr;\n visitStack.push_back(temp);\n popped = false;\n }\n else {\n if (not popped and currentSum == targetSum) {\n return true;\n }\n else {\n currentSum -= visitStack.back()->val;\n visitStack.pop_back();\n popped = true;\n }\n }\n }\n return false;\n }\n};", "memory": "14600" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n\n if (!root)\n return false;\n \n if (root->val == targetSum && !root->left && !root->right)\n return true;\n \n stack<TreeNode*> tree_stack;\n\n TreeNode * current = root;\n int current_sum = current->val;\n bool going_back = false;\n\n while (true)\n {\n if (current->left)\n {\n tree_stack.push(current);\n current = current->left;\n current_sum += current->val;\n going_back = false;\n }\n else\n if (current->right)\n {\n tree_stack.push(current);\n current = current->right;\n current_sum += current->val; \n going_back = false;\n }\n else\n {\n if (tree_stack.empty())\n {\n return false;\n }\n else\n if (current_sum == targetSum && !current->left && !current->right && !going_back)\n {\n return true;\n }\n\n current_sum -= current->val;\n TreeNode * parent = tree_stack.top();\n tree_stack.pop();\n going_back = true;\n\n if (current == parent->left)\n {\n delete current;\n current = parent;\n current->left = nullptr;\n }\n else\n if (current == parent->right)\n {\n delete current;\n current = parent;\n current->right = nullptr;\n }\n }\n }\n }\n};", "memory": "16000" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n\n if (!root)\n return false;\n \n if (root->val == targetSum && !root->left && !root->right)\n return true;\n \n stack<TreeNode*> tree_stack;\n\n TreeNode * current = root;\n int current_sum = current->val;\n bool going_back = false;\n\n while (true)\n {\n if (current->left)\n {\n tree_stack.push(current);\n current = current->left;\n current_sum += current->val;\n going_back = false;\n }\n else\n if (current->right)\n {\n tree_stack.push(current);\n current = current->right;\n current_sum += current->val; \n going_back = false;\n }\n else\n {\n if (tree_stack.empty())\n {\n return false;\n }\n else\n if (current_sum == targetSum && !current->left && !current->right && !going_back)\n {\n return true;\n }\n\n current_sum -= current->val;\n TreeNode * parent = tree_stack.top();\n tree_stack.pop();\n going_back = true;\n\n if (current == parent->left)\n {\n delete current;\n current = parent;\n current->left = nullptr;\n }\n else\n if (current == parent->right)\n {\n delete current;\n current = parent;\n current->right = nullptr;\n }\n }\n }\n }\n};", "memory": "16100" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n\n if (!root)\n return false;\n \n if (root->val == targetSum && !root->left && !root->right)\n return true;\n \n stack<TreeNode*> tree_stack;\n\n TreeNode * current = root;\n int current_sum = current->val;\n bool going_back = false;\n\n while (true)\n {\n if (current->left)\n {\n tree_stack.push(current);\n current = current->left;\n current_sum += current->val;\n going_back = false;\n }\n else\n if (current->right)\n {\n tree_stack.push(current);\n current = current->right;\n current_sum += current->val; \n going_back = false;\n }\n else\n {\n if (tree_stack.empty())\n {\n return false;\n }\n else\n if (current_sum == targetSum && !current->left && !current->right && !going_back)\n {\n return true;\n }\n\n current_sum -= current->val;\n TreeNode * parent = tree_stack.top();\n tree_stack.pop();\n going_back = true;\n\n if (current == parent->left)\n {\n delete current;\n current = parent;\n current->left = nullptr;\n }\n else\n if (current == parent->right)\n {\n delete current;\n current = parent;\n current->right = nullptr;\n }\n }\n }\n }\n};", "memory": "16100" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n if(root==NULL){\n return false;\n }\n\n if(root->val==targetSum && root->left==NULL && root->right==NULL){\n return true;\n }\n targetSum-=root->val;\n return hasPathSum(root->left,targetSum) || hasPathSum(root->right,targetSum);\n }\n};", "memory": "19600" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool f(TreeNode* root, int sum , int targetSum){\n if(root == NULL)\n return false;\n \n sum += root->val;\n //if we reach at leaf node\n if(root->left == NULL && root->right == NULL){\n if(sum == targetSum)\n return true;\n else\n return false;\n }\n bool ls = f(root->left, sum , targetSum);\n bool rs = f(root->right, sum , targetSum);\n return ls|| rs;\n }\n bool hasPathSum(TreeNode* root, int targetSum) {\n int sum = 0;\n bool result = f(root, sum ,targetSum);\n return result;\n }\n};", "memory": "19700" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n if(root==NULL){return false;}\n if((root->right==NULL)&&(root->left==NULL)){\n return (root->val)==targetSum;\n }\n int j= targetSum-(root->val);\n return (hasPathSum(root->left,j))||(hasPathSum(root->right,j));\n }\n};", "memory": "19700" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool isleaf(TreeNode * node) {\n return node->left == nullptr && node->right == nullptr;\n }\n bool hasPathSum(TreeNode* root, int targetSum) {\n if(root == nullptr)\n return false;\n\n targetSum -= root->val;\n if(targetSum == 0 && isleaf(root))\n return true;\n\n bool ret = false;\n if(root->left != nullptr)\n ret |= hasPathSum(root->left, targetSum);\n if(root->right != nullptr)\n ret |= hasPathSum(root->right, targetSum);\n return ret;\n }\n};", "memory": "19800" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool preorder(TreeNode* root, int targetSum,int maxi)\n {\n if(root==NULL)\n return false;\n maxi=maxi+root->val;\n if(root->left==NULL && root->right==NULL)\n {\n if(maxi==targetSum)\n {\n return true;\n }\n else\n return false;\n }\n return preorder(root->left,targetSum,maxi) || preorder(root->right,targetSum,maxi);\n }\n\n bool hasPathSum(TreeNode* root, int targetSum) {\n if(root==NULL)\n {\n return NULL;\n }\n int maxi=0;\n return preorder(root,targetSum,maxi);\n\n }\n};", "memory": "19800" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n // Base case: if root is null, there is no path\n if (!root) {\n return false;\n }\n\n // Check if we've reached a leaf node and if the remaining target sum equals the node's value\n if (!root->left && !root->right) {\n return targetSum == root->val;\n }\n\n // Recursively check the left and right subtrees, reducing the target sum\n bool left = hasPathSum(root->left, targetSum - root->val);\n bool right = hasPathSum(root->right, targetSum - root->val);\n\n // Return true if either the left or right subtree has a valid path\n return left || right;\n }\n};\n", "memory": "19900" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n // just keep subtracting until we reach end\n if (root==NULL){\n return false;\n }\n if (root->val==targetSum && !root->left && !root->right){//it needs to be a leaf too\n return true;\n } \n return hasPathSum(root->left, targetSum - root->val) || hasPathSum(root->right, targetSum - root->val); // any one \n }\n};", "memory": "19900" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &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 bool hasPathSum(TreeNode* root, int targetSum) {\n if(root == nullptr){\n return 0;\n }\n if(root->left == nullptr && root->right == nullptr){\n return (root->val == targetSum);\n }\n targetSum = targetSum - root->val;\n return hasPathSum(root->left, targetSum) || hasPathSum(root->right, targetSum);\n }\n};", "memory": "20000" }