Easy to understand Java Solution using two queues

interview-questions
programming
Tags: #<Tag:0x00007f1828309638> #<Tag:0x00007f18283094f8>

#1
public void connect(TreeLinkNode root) {
    Queue<TreeLinkNode> parentQueue = new ArrayDeque<>();
    Queue<TreeLinkNode> siblingsQueue = new ArrayDeque<>();
    parentQueue.add(root);
    while (!parentQueue.isEmpty()){
        TreeLinkNode lastPop = parentQueue.poll();
        if(lastPop.left!=null){
            siblingsQueue.add(lastPop.left);
        }
        if(lastPop.right!=null){
            siblingsQueue.add(lastPop.right);
        }
        if(parentQueue.isEmpty()){
            lastPop.next = null;
            parentQueue.addAll(siblingsQueue);
            siblingsQueue.clear();
        }else{
            lastPop.next = parentQueue.peek();
        }
    }

}