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.
Initially, all the nextRight pointers point to garbage values. Your function should set these pointers to point next right for each node.
Input Tree A / \ B C / \ \ D E F Output Tree A--->NULL / \ B-->C-->NULL / \ \ D-->E-->F-->NULL
Method 1 (Extend Level Order Traversal or BFS)
Consider the method 2 of Level Order Traversal. The method 2 can easily be extended to connect nodes of same level. We can augment queue entries to contain level of nodes also which is 0 for root, 1 for root’s children and so on. So a queue node will now contain a pointer to a tree node and an integer level. When we enqueue a node, we make sure that correct level value for node is being set in 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.
Please refer connect Nodes at same Level (Level Order Traversal) for implementation.
Time Complexity: O(n)
Method 2 (Extend Pre Order Traversal)
This approach works only for Complete Binary Trees. In this method we set nextRight in Pre Order fashion to make sure that the nextRight of parent is set before its children. When we are at node p, we set the nextRight of its left and right children. Since the tree is complete tree, nextRight of p’s left child (p->left->nextRight) will always be p’s right child, and nextRight of p’s right child (p->right->nextRight) will always be left child of p’s nextRight (if p is not the rightmost node at its level). If p is the rightmost node, then nextRight of p’s right child will be NULL.
Following are populated nextRight pointers in the tree(-1 is printed if there is no nextRight) nextRight of 10 is -1 nextRight of 8 is 2 nextRight of 2 is -1 nextRight of 3 is -1
Thanks to Dhanya for suggesting this approach.
Time Complexity: O(n)
Why doesn’t method 2 work for trees which are not Complete Binary Trees?
Let us consider following tree as an example. In Method 2, we set the nextRight pointer in pre order fashion. When we are at node 4, we set the nextRight of its children which are 8 and 9 (the nextRight of 4 is already set as node 5). nextRight of 8 will simply be set as 9, but nextRight of 9 will be set as NULL which is incorrect. We can’t set the correct nextRight, because when we set nextRight of 9, we only have nextRight of node 4 and ancestors of node 4, we don’t have nextRight of nodes in right subtree of root.
1 / \ 2 3 / \ / \ 4 5 6 7 / \ / \ 8 9 10 11
See Connect nodes at same level using constant extra space for more solutions.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Connect Nodes at same Level (Level Order Traversal)
- Connect nodes at same level using constant extra space
- Difference between sums of odd level and even level nodes of a Binary Tree
- Sum of leaf nodes at minimum level
- Level with maximum number of nodes
- Level with maximum number of nodes using DFS in a N-ary tree
- Swap Nodes in Binary tree of every k'th level
- Sum of nodes at k-th level in a tree represented as string
- Count the number of nodes at given level in a tree using BFS.
- Product of nodes at k-th level in a tree represented as string
- Print nodes between two given level numbers of a binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Connect n ropes with minimum cost
- Non-crossing lines to connect points in a circle
- Delete N nodes after M nodes of a linked list