109. Convert Sorted List to Binary Search Tree || Code + Explanation + Example Walk through

preview_player
Показать описание
Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Рекомендации по теме
Комментарии
Автор

I literally feel fucked up when i didn't get the intution behind this problem.After saw this video i feel good.. You are my lifesaver ❤️

santhoshsiva
Автор

Note :- Pass the vector by reference to avoid vector copy in each recursive call.

ethicalgaming
Автор

Vector m store karke thodi na karna hai madam

motivationtipsknowledge
Автор

easy explanation loved it but takes 416ms of runtime compared to 13ms of top solution and 360mb of memory consumed
a better approach would be using slow and fast pointer method to find the middle of the linked list
thanks for this video

TreeNode* fun(ListNode* l, ListNode* r)
{
ListNode *s=l, *f=l;

if(l==r)
return NULL;
while(f!=r && f->next!=r)
{
s=s->next;
f=f->next->next;
}
TreeNode* t=new TreeNode(s->val);
t->left=fun(l, s);
t->right=fun(s->next, r);
return t;
}
TreeNode* sortedListToBST(ListNode* head) {
if(head==NULL)
return NULL;
if(head->next==NULL)
return new TreeNode(head->val);
return fun(head, NULL);
}

sahilbani
Автор

why this algorithm is working any intuition behind this...?

BHARATKUMAR-jljp
Автор

Can we do this without using extra space?

sauravchandra
Автор

it is not accepted in gfg when the input is 2 i.e. 2 nodes are there
please give solution for it

maragmodh
Автор

its doesn't seems the optimal solution since you are using extra space

technosujeet
Автор

Great Video thank u so much for your Efforts& time .
How to implement this in c#
The first part when i get defined the node from this recursiv method ..?

انامسلموكفى
join shbcf.ru