Skip to content

Instantly share code, notes, and snippets.

@varaprasadh
Created August 22, 2021 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varaprasadh/45a08ceb58f0e0a3901414fbc80dbf92 to your computer and use it in GitHub Desktop.
Save varaprasadh/45a08ceb58f0e0a3901414fbc80dbf92 to your computer and use it in GitHub Desktop.
class Solution {
public Node connect(Node root) {
if(root == null) return null;
Queue<Node> queue = new LinkedList();
queue.add(root);
while(queue.size()>0){
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.add(node.left);
if(node.right !=null) queue.add(node.right);
}
}
return root;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment