Skip to main content
Practice

Coding Quiz - Find Minimum Odd Value

In this coding quiz, your task is to write a function that utilizes a Binary Search Tree (BST) to find the smallest odd value from a given list of integers.

The function should insert the integers from the input list into a BST and then return the smallest odd value found in the list.

For instance, if the list [30, 20, 40, 10, 25, 35, 45] is provided as input, the solution function should return 25.


Write your solution code
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

def insertIntoBST(root, val):
if not root:
return TreeNode(val)
if val < root.val:
root.left = insertIntoBST(root.left, val)
else:
root.right = insertIntoBST(root.right, val)
return root


def findMinValue(root, condition):
if not root:
return float("inf")
if condition(root.val):
return min(
root.val,
findMinValue(root.left, condition),
findMinValue(root.right, condition),
)
else:
return min(
findMinValue(root.left, condition), findMinValue(root.right, condition)
)

def solution(numbers):
# Write your code here
return



Constraints

  • Assume there are no duplicate integers in the input list.

  • The BST does not allow duplicate values.

  • The input list is non-empty and contains at least one integer.

  • If there are no nodes that meet the condition, the function should return None.




Example

  • Input: [10, 5, 15, 3, 7, 13, 18]

  • Output: 3

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.