Please read the instructions carefully before starting the exam.
Once you start the exam, the timer will begin, and you cannot pause it.
Ensure that you complete and submit your code within the given time if the timer is enabled.
In a smart contact organiser app, names are stored in a Binary Search Tree (BST) to allow efficient alphabetical lookup.
Your task is to implement a function that finds the closest matching contact name in the BST to a given target name based on alphabetical order.
If multiple names are equally close, return the one that appears first in in-order traversal.
tree: A list of strings representing level-order traversal of a BST. Use "null" for missing nodes.target: A string representing the name you're trying to match.Return the contact name in the BST that is alphabetically closest to the target.
Input: [Emma, David, Sophia], Ella
Output: Emma
Explanation:
Tree structure:
Emma
/ \
David Sophia
Closest alphabetically to "Ella" is "Emma".
Input: [Kevin, Jake, Mike, null, null, Louis], Lucas
Output: Louis
Explanation:
Kevin
/ \
Jake Mike
/
Louis
"Lucas" is alphabetically closest to "Louis".
Input: [Zoe, Ann], Aaron
Output: Ann
Explanation:
Ann is the only node alphabetically near Aaron.
Input: [Mia, Emma, Olivia, null, null, Lily], Leo
Output: Lily
Explanation:
Mia
/ \
Emma Olivia
/
Lily
"Leo" is closest alphabetically to "Lily".