Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You are given a BST (Binary search tree) with’ N’ number of nodes and a value ‘X’. Your task is to find the greatest value node of the BST which is smaller than or equal to ‘X’.

Note :‘X’ is not smaller than the smallest node of BST .

For example:

```
In the above example, For the given BST and X = 7, the greatest value node of the BST which is smaller than or equal to 7 is 6.
```

Detailed explanation

```
1 <= T <= 5
1 <= N <= 5 * 10 ^ 3
1 <= nodeVal[i] <= 10 ^ 9
Time Limit: 1 sec.
```

```
2
10 5 15 2 6 -1 -1 -1 -1 -1 -1
7
2 1 3 -1 -1 -1 -1
2
```

```
6
2
```

```
In the first test case, the BST looks like as below:
```

```
The greatest value node of the BST which is smaller than or equal to 7 is 6.
In the second test case, the BST looks like as below:
```

```
The greatest value node of the BST which is smaller than or equal to 2 is 2.
```

```
2
5 3 10 2 4 6 15 -1 -1 -1 -1 -1 -1 -1 -1
15
5 3 10 2 4 6 15 -1 -1 -1 -1 -1 -1 -1 -1
8
```

```
15
6
```