Problem of the day
Your friends gifted you a lot of things on your birthday, and now it's your turn to give some return gifts to them. You went to a gift store and decided to buy some Binary Search Trees (BST).
There is no salesperson in the store. So you are supposed to guess the price of a given BST, which is the minimum value in its nodes.
The first line contains 'T', denoting the number of test cases.
For each Test :
The first line contains an integer, 'N'.
The second line contains an array 'A' of 'N' space-separated integers, with a positive integer representing a node and -1 representing a NULL value.
The input array 'A' denotes Level Order traversal of the BST.
(Note that 'N' is not the number of nodes in the BST, only positive integers in 'A' denote nodes of BST).
Output Format:
For each test case, print one integer, denoting the price of a given BST, i.e., minimum node value in it.
Note:
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= 'T' <= 10
1 <= 'N' <= 10^5
1 <= A[i] <= 10^6 or A[i] = -1, i ∈ (1, N)
Note - The sum of 'N' over all test cases does not exceed 2 * 10^5.
Time Limit: 1 sec
2
13
5 4 6 3 -1 -1 7 1 -1 -1 -1 -1 -1
7
9 -1 10 -1 11 -1 -1
1
9
For Case 1:
Given input looks like the tree drawn above. It can be seen that the minimum value in the tree is 1.
For Case 2:
Given input looks like the tree drawn above. It can be seen that the minimum value in the tree is 9.
2
7
20 8 22 -1 -1 -1 -1
15
40 9 50 4 12 -1 -1 -1 -1 10 14 -1 -1 -1 -1
8
4
Can we recursively traverse to visit all the nodes of the given BST?
Approach: Maintain a variable to store the minimum value in the tree. Do any traversal of your choice (Inorder, Preorder, Postorder, etc.) on the tree, and for each node, update the variable to keep track of the minimum value.
Algorithm:
O(N), where 'N' is the number of nodes in the BST.
Any traversal (Inorder, Preorder, Postorder) on the tree, iterate each tree node once. Hence, overall time complexity becomes O(N).
O(H), where 'H' is the height of the given tree.
Recursion stack keeps track of nodes from root to leaf, and the count of these nodes cannot exceed the tree's height. Hence, overall space complexity becomes O(H).