Populating Next Right Pointers in Each Node II | Live Coding with Explanation | Leetcode - 117

preview_player
Показать описание
Detailed explanation for Populating Next Right Pointers in Each Node Leetcode problem 117.
Check out the Populating Next Right Pointers in Each Node 2 here:

To support us you can donate

Check out our other popular playlists:

0:00 Basic BFS
2:40 Follow up [ constant space O(1) ]

If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful.

#coding #leetcode #programminglife #programmingisfun #programmer #tech #software #codinglife #leetcode
Рекомендации по теме
Комментарии
Автор

We hope you all are enjoying our videos!!! Don't forget to leave a comment!!! Please like the video to support us!!!

Questions you might like:

Struggling in a question??

Leave in a comment and we will make a video!!!🙂🙂🙂

AlgorithmsMadeEasy
Автор

This was indeed a tricky problem. The trick of the problem is that we are traversing two levels at a time: Current level and the children of the current level. Introduction of dummy variable in the code before explaining it made it harder for me to understand it. After spending enough time on it, I see it though. If possible, try and match the code with the explanation. I do see that you go back and explain the dummy variable but by that time, viewer is confused already by seeing it in the code. Also, calling the variable with a meaningful name like `childHead` might have made it easier to understand. Good explanation, regardless.

subhedarsanchay
Автор

Wonderful explanation. Felt the explanation is easier than most of LeetCode discussions and official solution. Not to mention it is highly efficient as well. Great stuff guys. Really great.

supreethpadavala
Автор

This answer is brilliant !!! Thank you for sharing!!! Let me do some explanation as things are not as straightforward for new learners like me:

outer while loop 1: only root is store in queue. The size will be 1, so in the inner while loop we just pop root from the queue and store it in the dummy (dummy = dummy.next). Here node 2 and 3 is added to q.
outer while loop 2: Now we go the next round on the q(2, 3), here dummy is redefined. It first point to node 2, and then in the next inner loop dummy points to 3 and 3 (dummy.next) is pointed to 2 (dummy). Here 4, 5, 7 is added to queue.
outer while loop 3: dummy.next->4, dummy->4; dummy.next->5, dummy(4)->dummy.next(5); dummy.next->7, dummy(5)->dummy.next(7)

xinniu
Автор

Excellent solution...better than the leet code's official solution, it was a little bit complex to understand and implement during the interview. thanks for sharing.

obulpathi
Автор

I am bit confused on space complexity. As you are creating a new dummy node (from heap space right?) for each outer while loop invocation, you will be creating O(h) dummy nodes (h is height of tree) and hence it is not constant space. Anything I am missing here?

ThePradeep
Автор

Could you please tell me why
head = dummy.next;
can get value even though you use p next = node.left ? (it seems that you don’t set value for dummy inside roop)
if we write TreeLinkNode p = dummy; then dummy could reference the p ? so it means both could take same values?

starpomero
Автор

This is brilliant! Superb explaination.

shreyash
Автор

I don't understand how dummy node is changing levels without updating dummy node anywhere?

mortalphoenix
Автор

thanks alott.. the leetcode solution hurt my brain. thanks again

Rahul-sqgk
Автор

Quick and lucid explanation, thank you!

sommayghosh
Автор

Nice explanation sir the follow up part is a bit tricky??

paragroy
Автор

Very well explained sir. You made the problem really easy. Thankyou sir.

toshanverma
Автор

Since a new node is being made at every level isn't the space complexity O(h) where h is the height of the tree?

ZeroTrunks
Автор

Thanks I was not able to come up with this solution

hasrat
Автор

You are using the outer while() to traverse the height and inner while() to traverse the level and creating a new node inside the outer while loop. SO in that case you are dynamically creating h nodes where h is the height of the space complexity in that case be O(height) instead of O(1)?

ratnadeepbhattacharya
Автор

Thank you so much sir!! You just made my day :)

vincechen
Автор

Cool video dude. How many years have you been coding by the way to get to this skill level?

FitnessChaos
Автор

how its working internally...the code u showed up was easy to understand bt how storing process done of each node?

obito
Автор

Space complexity of BFS is O(N), while you should use O(1) in this problem

Gia
visit shbcf.ru