Populating Next Right Pointers in Each Node | LeetCode | Java

RMAG news

class Solution {
public Node connect(Node root) {

if(root==null)
return root;

Queue<Node> queue = new LinkedList<>();

queue.add(root);

while(!queue.isEmpty()){
int size = queue.size();
for(int i=0; i<size; i++){
Node node = queue.remove();

if(i<size1)
node.next = queue.peek();

if(node.left != null)
queue.add(node.left);

if(node.right!=null)
queue.add(node.right);

}
}

return root;
}
}

Thanks for reading 🙂
Feel free to comment and like the post if you found it helpful
Follow for more 🤝 && Happy Coding 🚀

If you enjoy my content, support me by following me on my other socials:
Github
Twitter(X)
Hashnode
Medium