Inorder Successors

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
0 upvote

Problem statement

Ninja has recently learnt about the heirarchial structure of Binary trees. Now, he wants to find the inorder successor of each node in a Binary Tree and want to implement that in a well mannered code.

Ninja is stucked in the problem and he is asking for your help. Help Ninja!

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:
2
3 5 1 6 2 0 8 -1 -1 7 4 -1 -1 -1 -1 -1 -1 -1 -1
1 2 3 4 5 -1 -1 6 -1 -1 -1 -1 -1
Sample Output 1:
6->5 5->7 7->2 2->4 4->3 3->0 0->1 1->8 8->-1 
6->4 4->2 2->5 5->1 1->3 3->-1 
Explanation For Sample Input 1:
Test  Case1 :
The above input would result in the below Binary Tree:

Inorder Traversal for the above tree will be:
6 -> 5 -> 7 -> 2 -> 4 -> 3 -> 0 -> 1 -> 8
So, inorder successor of each node will be:
6->5 5->7 7->2 2->4 4->3 3->0 0->1 1->8 8->-1 

Test Case 2:
The above input would result in the below Binary Tree:

Inorder Traversal for the above tree will be:
6 -> 4 -> 2 -> 5 -> 1 -> 3
So, inorder successor of each node will be:
6->4 4->2 2->5 5->1 1->3 3->-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
Approaches (1)
Reverse Inorder Traversal
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Inorder Successors
Full screen
Console