Populating Next Right Pointers in Each Node | LeetCode | Java
Tanuja V
Posted on June 8, 2024
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
💖 💪 🙅 🚩
Tanuja V
Posted on June 8, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
javascript Day 5 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#56.Merge Intervals(Medium/JavaScript)
February 15, 2022