Populate Inorder Successor of all nodes of a Binary Tree

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
6 upvotes
Asked in company
Accolite

Problem statement

You have been given a Binary Tree. You are supposed to populate the next pointer for all the nodes. The next pointer for every node should be set to point to the inorder successor.

Note:

The Inorder Successor of a node in a binary tree is the next node in Inorder traversal of the Binary Tree. It is supposed to be NULL for the last node in Inorder traversal, for which suppose it to be ‘-1’.
For example :
For the given binary tree:

Example

The inorder traversal of the tree will be: 4 -> 2 -> 5 -> 1 -> 6 -> 3 -> 7
Hence, the inorder successor of each node will be: 4 -> 2 2 -> 5 5 -> 1 1 -> 6 6 -> 3 3 -> 7 7 -> -1
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases are as follows.

The first line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.

For example, the input for the tree depicted in the below image would be :

Example

Input Format:   
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation :

Level 1 :
The root node of the tree is 1

Level 2 :
Left child of 1 = 2
Right child of 1 = 3

Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6

Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)

Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null(-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree. 
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format:
For each test case, print the data for both nodes separated by ‘->’, in case if the node has no successor, print ‘-1’. 

Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. You just need to complete the function.
Constraints:
1 <= T <= 100
0 <= N <= 3000
1 <= Data <= 10^4

Time limit: 1 sec
Sample Input 1:
1
10 8 12 3 -1 -1 -1 -1 -1
Sample Output 1:
3->8 8->10 10->12 12->-1    
Explanation For Sample Input 1:
The above input would result in the below Binary Tree:

Example

Inorder Traversal: 3 -> 8 -> 10 -> 12
So, output => 3 -> 8 8 -> 10 10 -> 12 12 -> -1
Sample Input 2:
1
5 7 1 4 3 2 -1 -1 -1 -1 -1 -1 -1
Sample Output 2:
4->7 7->3 3->5 5->2 2->1 1->-1
Hint

Can you think of an unorthodox method to traverse through the tree?

Approaches (1)
Reverse Inorder Traversal

The basic idea is to traverse the given tree in reverse inorder traversal and keep track of previously visited nodes. Reverse Inorder traversal is a modified version of Inorder Traversal, in which the basic idea of inorder traversal remains the same. The difference is that in reverse Inorder traversal we traverse the right subtree first and then the left subtree.

 

Reverse Inorder Traversal:

 

  1. First, traverse the right subtree.
  2. Then, traverse the root.
  3. Finally, traverse the left subtree.

 

 Algorithm:

 

  • Set next of node and all the descendants of the node by traversing them in reverse inorder.
    • The first visited node will be the rightmost node.
      • Next of this rightmost node will be NULL.
    • If the current node is not NULL:
      • First, set the next pointer in the right subtree.
      • Set the next as the previously visited node in reverse order.
      • Change the value of previous for the subsequent node.
      • Finally, set the next pointer in the right subtree.
Time Complexity

O(N), where ‘N’ is the total number of nodes in the binary tree.

 

Since we are visiting each node exactly once, the overall time complexity will be O(N).

Space Complexity

O(1)

 

Since we are not using any extra space, the overall space complexity will be O(1).

Code Solution
(100% EXP penalty)
Populate Inorder Successor of all nodes of a Binary Tree
Full screen
Console