[Python 3] Tree Solution (100% time 100% space)
When you first look at this problem, you already know that you have to use binary search when searching for the ideal room for each query because linear search 
When you first look at this problem, you already know that you have to use binary search when searching for the ideal room for each query because linear search is trivial to implement.\n\nThe difficult part is figuring out how to perform binary search on the subset of rooms where room_size >= preferred_size and where that subset is sorted by room_id.\n\nThe key to solving the above statement requires the following steps:\n\n1. Sort the rooms and queries by room size in descending order. This allows us to grow our subset of rooms to search without having to remove any rooms. (Example: a query with preferred size 1 will always include all the rooms for a query with preferred size 2)\n2. When iterating through each query, you will add the new rooms into your search data structure. It is necessary for this data structure to have O(logn) insert and O(logn) search. That is a binary tree. If you want to get fancy, you can say AVL tree.\n3. Search the binary tree for the best room_id and add it to your result array  \n\n\n```\nclass Node:\n    def __init__(self, val):\n        self.val = val\n        self.left = None\n        self.right = None\n        \nclass Solution:\n    def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:\n\t\t# sort by room size\n\t\trooms.sort(key=lambda x: -x[1])\n        queries = sorted([[q[0], q[1], i] for i, q in enumerate(queries)], key=lambda x: -x[1])\n        \n\t\t# pointer for iterating through your rooms in one pass\n        r = 0\n        \n        root = None\n        res = [-1] * len(queries)\n\n        # pref size is going to be in descending order\n        for pref_id, pref_size, index in queries:\n            \n\t\t\t# add room_ids to your tree data structure\n            while r < len(rooms) and rooms[r][1] >= pref_size:\n                room_id = rooms[r][0]\n                if root is None:\n                    root = Node(room_id)\n                else:\n                    insert(root, room_id)\n                    \n                r += 1\n\n\t\t\t# search your tree data structure for best room_id\n            if root is not None:\n                res[index] = search(root, pref_id, math.inf)\n        \n        return res\n    \ndef insert(root, val):\n    # each val is guaranteed to be unique\n    if val < root.val:\n        if root.left is None:\n            root.left = Node(val)\n            return\n        else:\n            return insert(root.left, val)\n    else:\n        if root.right is None:\n            root.right = Node(val)\n            return\n        else:\n            return insert(root.right, val)\n\ndef search(root, val, res):\n    if abs(val - root.val) < abs(val - res):\n        res = root.val\n        \n    elif abs(val - root.val) == abs(val - res):\n        res = min(res, root.val)\n    \n    if val < root.val:\n        if root.left is None:\n            return res\n        else:\n            return search(root.left, val, res)\n    else:\n        if root.right is None:\n            return res\n        else:\n            return search(root.right, val, res)\n```
['Tree', 'Python', 'Python3']