How to connect nodes at same level using Queue? ORPopulate next right pointers for each Tree Node? ORConnect nodes of a binary tree at same Level?

Let us first understand what we want to achieve? what is the input and what will be the expected output?

Question: Binary Tree is given to you, 1. some node of tree has both left and right child present,2. some node of tree has only left child present and3. some node of tree has only right child present, 4.nextRight pointer of all the node initially is null.Our task is to connect nextRight pointer of each node to its adjacent node. 1. If the immediate adjacent node is not present then connect to next adjacent node and 2. If next adjacent node is not present then connect to next to next adjacent node and 3. If
next to next adjacent node is not present then search until you find
the adjacent node along the same Level and connect to it.4. If the adjacent node is not present at same level then connect it to null.

What is adjacent node?

Look at the below elaborated Case 2 image, Immediate adjacent node of Node 7 is not present as Node 3 doesn't has right child. Next to adjacent node is also not present as Node 8 doesn't has left child, Also next to next adjacent node is not present as Node 8 doesn't has right child.

While searching along the same Level, we see Node 25 is present and it has right child present, So connect Node 7 nextRight pointer to Node 30.

Algorithm

If we know how to traverse the Tree level by level from Left to Right, then we can very well connect nodes to its adjacent node by extending the logic..

Solution:1.What first thing we have to do is to take a Queue for reading the tree in Level order fashion.2. Read all nodes at the same level and enQueue it in queue,

3. Once all the nodes of a same level are read and enqueued in queue. add some notation in a queue which represent the end of level, otherwise we will not come to know where one level has started and ended in queue. so we will add dummy entry NULL

4.Now, once the queue is filled with Level 1, Queue will look like below image,

we will not able to read other levels until and unless we will read Level 1 data already present in the Queue (because data present in Level 2 that is Node 5 and Node 20 is actually Left and Right child of Node 10, which is present in Queue).

If you observe in the Binary Tree, Node 10 nextRight pointer is connecting to null. From the Queue, we can see the relation that if we connect nextRight pointer of Node present at head (Node 10) of a queue to the Node present just after it (null), then Node 10 nextRight pointer will get connected to null and our task for first level is completed. For inserting level 2 data in queue, what we will do is, once we read the Node from the Queue (in our case Node 10 is first node we read) put the Left and Right child (if present) of that node in the queue. After deQueue Node 10 from queue and putting its child node 5 and 20 in queue, Queue will look like below image,

the next node in the queue we will encounter is null.

which signifies the end of 1st level and

It also signifies that all left and right child of 1st level nodes and done putting in the queue,

and Queue is now filled with Level 2 nodes, So we have to putt null node at the end of the queue

for identification that Level 2 nodes is getting completed at that point and whatever added after

that null will be part of Level 3 nodes.Queue will look like below image after reading null in above queue,

Repeat step 3 and remember the basic rule,

Node node = queue.dequeue(); node.setNextRight(elementAtHeadOfQueue);

If node is null it signifies end of level and If node is not null then simply do node.setNextRight(elementAtHeadOfQueue); and put left and right child of node in queue. Overview:

Java Program for Connecting nodes at Same Level in Binary Tree.(Extend Level Order Traversal or BFS approach)

Post a Comment

I'm Jayesh Patel, author of "JavaByPatel". I'm not a professional blogger but when time permits, love to share in-depth solutions to common Interview questions asked.Any questions/feedback, Please drop a mail atjayeshmaheshpatel@gmail.com