connected to right nodes which are singletons course hero

by Vergie Farrell 10 min read

How to connect nodes at same level (level order traversal)?

We initialize a node Prev which points at the previous node. While traversing the nodes in the same level we keep track of the previous node and point the nextRight pointer to the current node in every iteration. Please refer connect Nodes at same Level (Level Order Traversal) for implementation.

How to connect adjacent nodes at the same level in binary tree?

Write a function to connect all the adjacent nodes at the same level in a binary tree. Structure of the given Binary Tree node is like following. // This code is contributed by importantly. Initially, all the nextRight pointers point to garbage values. Your function should set these pointers to point next right for each node.

How to set nextright of a node in a queue?

To set nextRight, for every node N, we dequeue the next node from queue, if the level number of next node is same, we set the nextRight of N as address of the dequeued node, otherwise we set nextRight of N as NULL. We initialize a node Prev which points at the previous node.

How do we keep track of the previous node while traversing?

We initialize a node Prev which points at the previous node. While traversing the nodes in the same level we keep track of the previous node and point the nextRight pointer to the current node in every iteration.