[Java] Leetcode 117. Populating Next Right Pointers in Each Node II [Binary Tree #4]

preview_player
Показать описание
In this video, I'm going to show you how to solve Leetcode 117. Populating Next Right Pointers in Each Node II which is related to Binary Tree.

Here’s a quick rundown of what you’re about to learn:

⭐️ Course Contents ⭐️
⌨️ (0:00) Question
⌨️ (1:53) Solution Explain
⌨️ (7:55) Code

In the end, you’ll have a really good understanding on how to solve Leetcode 117. Populating Next Right Pointers in Each Node II and questions that are similar to this Binary Tree.

Рекомендации по теме
Комментарии
Автор

The simplicity and clarity of the solution is insane! I was stuck on how to do the in-order traversal but forgot that the last level traversed was already linked but could move to the next one by simply head.next and just add != null to the while loop

dannyyin
Автор

good explanation, the best found on this problem! Thanks, Eric!

ChanChan-pgwu
Автор

Thanks for this, great solution. Other videos on here were 17 minutes long for some reason. It's nice to get an explanation that is straight forward and clean looking

traviszito
Автор

how you just got 5k subscription? the most clear explanation I've ever seen!

linjiafu
Автор

If this problem can use extra space then we can write like that: FYI
public Node connect(Node root) {
Node head = root;
if(root == null){
return null;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
Node node = queue.poll();
if(i < size - 1){
node.next = queue.peek();
}
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
}
return root;
}

MeetManga
Автор

How can dummy.next point to next level? We are changing temp.next right? Could you explain?

fireflyeeeee
Автор

great solution.. since you just created dummy node, how it points to the first element of next level.. i.e how dummy.next is pointing to the left of the root or start of the next level... is it because of temp node?

praveen
join shbcf.ru