3

Diagonal Traversal

Difficulty: MEDIUM
Avg. time to solve
20 min
Success Rate
80%

Problem Statement
Suggest Edit

You have been given a binary tree of integers. You have to return all the diagonal paths of the binary tree. A diagonal path is one in which all the nodes pass through -1 slope line.

A binary tree is a tree in which each parent node has at most two children.

Note:

Order of return of diagonal path’s array/vector: The rightmost diagonal path must come first, and so on.
Every parent node comes first then the child node. In other words, return the diagonal element from top to bottom.

Example

Consider the given binary tree.

subsequence

There are 4 diagonal paths:
1 3 6
2 5 9
4 8
7
You need to return ‘1 3 6 2 5 9 4 8 7’.

Let's consider this example

subsequence

Diagonal paths are:
1 3 6
2 5
4

You need to return ‘1 3 6 2 5 4’.
Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.

The first line of input contains the elements of the tree in the level order form separated by a single space.
If any node does not have a left or right child, take -1 in its place. Refer to the example below.

Example:

Elements are in the level order form. The input consists of values of nodes separated by a single space in a single line. 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 :

alt text

 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 every test case, the output prints in a single line the diagonal paths of the given binary tree.

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just return an array/list of all diagonal paths in which the rightmost diagonal path comes first, the second rightmost diagonal path comes second, and so on.
Constraint :
1 <= T <= 10^2
1 <= N <= 3*10^3
-10^9 <= DATA <= 10^9

Where ‘N’ is the number of nodes in the tree, and 'DATA' denotes data contained in the node of a binary tree.

Time Limit: 1 sec
Sample Input 1:
2
1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
1 2 3 -1 -1 4 5 -1 -1 -1 -1
Sample Output 1:
 1 3 7 2 5 6 4
 1 3 5 2 4
Explanation For Sample Test 1:

alt text

It is clear that '1 3 7' is the first diagonal path '2 5 6' is the second and '4' is the third diagonal path. But 5 and 6 have the same level and same diagonal path, so we need to consider a node that comes first in the pre-order traversal.
Hence the diagonal path of the above binary tree is 1 3 7 2 5 6 4.

Test case 2:

alt text

It is clear that the first diagonal path is '1 3 5', and the second is '2 4'.
Hence diagonal binary tree traversal when combined is '1 3 5 2 4'.
Sample Input 2:
2
1 -1 2 3 4 -1 -1 -1 -1
1 2 3 -1 -1 4 -1 -1 -1
Sample Output 2:
1 2 4 3
1 3 2 4
Reset Code
Full screen
copy-code
Console