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<size-1)
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:
https://linktr.ee/tanujav7
Top comments (0)