Want to solve this problem? Login now to get access to solve the problems

Problem

Submissions

Solution

Leaderboard

Coming soon

4

Difficulty: EASY

Avg. time to solve

15 min

Success Rate

85%

Problem Statement

Suggest Edit

```
The first line of input contains an integer ‘T’ denoting the number of test cases.
Each test case follows.
The first line of every test case contains elements 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 on its place.
The second line contains two space-separated integers denoting node1 and node2.
For example, the input for the tree depicted in the below image would be :
```

```
20
10 35
5 15 30 42
-1 13 -1 -1 -1 -1 -1
-1 -1
Explanation:
Level 1 :
The root node of the tree is 20
Level 2 :
Left child of 20 = 10
Right child of 20 = 35
Level 3 :
Left child of 10 = 5
Right child of 10 = 15
Left child of 35 = 30
Right child of 35 = 42
Level 4 :
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 15 = 13
Right child of 15 = null (-1)
Left child of 30 = null (-1)
Right child of 30 = null (-1)
Left child of 42 = null (-1)
Right child of 42 = null (-1)
Level 5 :
Left child of 13 = null (-1)
Right child of 13 = 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:
20 10 35 5 15 30 42 -1 13 -1 -1 -1 -1 -1 -1 -1
```

```
For each test case, return the maximum element in the path from node1 to node2. If either node1 or node2 does not exist in the BST, return -1.
```

```
The path from node1 to node2 does not include node1 and node2.
node1 and node2 are unique.
If there is no element in the path from node1 to node2, return -1.
You don’t have to print anything; it has already been taken care of. Just complete the function.
```

```
1 <= T <= 100
1 <= N <= 3000
0 <= data <= 10^9
1<= node1, node2 <= 10^9
Where ‘T’ is the total number of test cases, data denotes the value of each node in the given tree and node1 and node2 are 2 integers.
Time limit: 1 second
```

```
1
5 3 7 1 4 6 9 -1 2 -1 -1 -1 -1 -1 -1 -1 -1
1 9
```

```
7
```

```
The BST of the given elements is shown below.
```

The path from 1 to 9 is shown in the curve. As we can see, the maximum element in the path from 1 to 9 is 7.

```
2
2 1 4 -1 -1 3 -1 -1 -1
3 5
7 5 8 -1 6 -1 10 -1 -1 9 -1 -1 -1
6 7
```

```
-1
5
```

```
Test case 1:
As 5 does not exist in the BST, simply return -1.
Test case 2:
After creating the BST for the given level order, we can see that the 5 is the maximum element between 6 and 7.
```

Want to solve this problem? Login now to get access to solve the problems