![]() |
VOOZH | about |
Given a binary tree, connect the nodes that are at the same level. Given an addition nextRight pointer for the same. Initially, all the next right pointers point to garbage values, set these pointers to the point next right for each node.
Examples:
Input:
👁 Connect-Nodes-at-same-Level-2-Output:
👁 Connect-Nodes-at-same-Level-Explanation: The above tree represents the nextRight pointer connected the nodes that are at the same level.
Table of Content
The idea is to use line by line level order traversal, we mainly count nodes at next level using queue size. As nodes are processed, each node's nextRight pointer is set to the next node in the queue.
Next Right of 8 is 2 Next Right of 3 is 4
Next Right of 8 is 2 Next Right of 3 is 4