Created
August 22, 2021 16:21
-
-
Save varaprasadh/45a08ceb58f0e0a3901414fbc80dbf92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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