Convert Sorted List to Binary Search Tree | Google | Flipkart | Amazon | Leetcode 109

preview_player
Показать описание
This is the 7th Video on our Linked List Playlist.
In this video we will try to solve another interesting and popular Linked List problem "Convert Sorted List to Binary Search Tree" (Leetcode-382).

Problem Name : Convert Sorted List to Binary Search Tree
Company Tags : Google, Flipkart, Amazon

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

Simply brilliant! Below is the Java Implementation (TC: O(NlogN) Beats 100%)

class Solution {
public TreeNode sortedListToBST(ListNode head) {

if(head == null) return null;

if(head.next == null) return new TreeNode(head.val);

ListNode slow = head;
ListNode fast = head;
ListNode slow_prev = null;

while(fast != null && fast.next != null) {
slow_prev = slow;
slow = slow.next;
fast = fast.next.next;
}

//slow points to mid
slow_prev.next = null;
TreeNode root = new TreeNode(slow.val);
root.left = sortedListToBST(head);
root.right = sortedListToBST(slow.next);

return root;
}

}

JJ-tpdd
Автор

Brother, found your channel randomly and now i'm hooked. Superb teaching 😍.

Whirlwind
Автор

Thanks for uploading full explanation. I have improved a lot and my interest to solve problem has also increased.

bhartipatel
Автор

Bhai thanku bhai koi doubt ho hi nahi sakta 100% trust hai bhai aap pe

jogindrasingh
Автор

Wow seriously I watch only 3 minutes of this video, and understood the complete approach, such beautifully you explain . Coded this myself . Thank you MIK

prateekmohanty
Автор

never thought this would be so easy... when GOD of DSA teaches, everything is just a story, , , hats off 2 u
one suggestion, why we are doing the slow_prev->next = NULL, this could had been explained more in depth, like if we dont do it, then middle node will be again considered as a part of BST, i understood this concept, but, someone might had not, if u can please focus/ explain this point of view as well, will be very helpful.

DurgaShiva
Автор

Bhaiya aap ka channel gfg ke problem statement ke comment section me randomly mil gaya tha and now i am hooked. Superb Teaching ❤

aatish
Автор

Wow you gave your 200% very easy explanation....Thank you very much for such effort..,

abhishekyadav
Автор

Amazing Explanation....I haven't seen much people who explain everything as clear as you do. Thanks for making such content :))

ashuthe
Автор

All the other playlists are awsome including this…great

SwikarP
Автор

Solved this question in O(N) instead of O(NlogN) but used space.
Code:

class Solution {
public:
TreeNode* createBST(vector<int>& nums, int l, int r)
{
if(l<=r)
{
int mid = (l+r)/2;
TreeNode* root = new TreeNode(nums[mid]);

root->left = createBST(nums, l, mid-1);
root->right = createBST(nums, mid+1, r);

return root;
}

return NULL;
}

TreeNode* sortedListToBST(ListNode* head) {
if(head == NULL)
return NULL;

//Storing the LinkedList in a vector
vector<int> nums;

while(head!=NULL)
{
nums.push_back(head->val);
head = head->next;
}

//Calling createBST() & returning the root of created tree.
return createBST(nums, 0, nums.size()-1);
}
};

molyoxide
Автор

Bhaiya aap ka 2no graph playlist best hai.
Thanku ❤

aatish
Автор

great explanation bhaiya. stack ke histogram and top problem ke bhi video banayiye bhaiya.

_rahulkumar
Автор

Learnt a new Thing, Really thanks a ton from bottom of my heart Bro.

molyoxide
Автор

Bhaiya please cover graph concepts playlist as well😀

jayanth
Автор

I think u will cross many big youtubers one day!! you should launch your own courses or some unique content(like your videos ) . by the way love for your videos.

yashaggarwal
Автор

It can be solved in O(N) time and O(logN) space complexity and thats what interviewers will expect.

elakstein
Автор

As always great explaination bhaiya.
Just a request can you make a video on "365. water and jug problem" . There is no good explaintion on that problem.
Some people are solving that problem with bfs other are solving with gcd. Just a request whenever you get time please make a video on it.

arjunsolanki
Автор

Awesomeee man, one of best explanation

MohdShahrukh-nenc
Автор

please also tell 2 pointer approach of this Q's

bhuppidhamii
visit shbcf.ru