Lecture
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
Lessons in this chapter · Review and Deepen Understanding of Python Data Structures
- 1. Review of Key Data Structures
- 2. Multiple-choice quiz
- 3. Fill-in-the-blank quiz
- 4. Coding Quiz - Reverse Words
- 5. Coding Quiz - Task Scheduling
- 6. Coding Quiz - Check for Value Existence
- 7. Coding Quiz - Find the First Non-repeating Character
- 8. What is a Tree?
- 9. Tree Implementation Techniques
- 10. Multiple-choice quiz
- 11. Coding Quiz - Find Minimum Odd Value
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help