Very Short and easy Java Solution using BFS


#1

public class Solution {
public void connect(TreeLinkNode root)
{
if(root == null)
return;
LinkedList q = new LinkedList<>();
q.add(root);
q.add(null);
while(!q.isEmpty())
{
TreeLinkNode curr = q.remove();
if(curr==null)
{
if(q.isEmpty())
break;
q.add(null);
continue;
}
curr.next = q.peek();
if(curr.left!=null)
q.add(curr.left);
if(curr.right!=null)
q.add(curr.right);
}

}

}