Problem 445710 · medium · Level 04 Non-Linear Data Structures

Clearing Out a Whole List of Keys

binary search trees · deletion · sequence

Remove each key of values from the search tree, in the order given, always using the symmetric successor for a node with two children. Keys that are not in the tree are skipped. Return the final tree.

Write remove_all(root, values).

Examples

Input:  root = build_tree([20, 10, 30, 5, 15, 25, 40]), values = [5, 20, 40]
Output: tree_to_list(...) == [25, 10, 30, None, 15]

Input:  root = build_tree([2, 1, 3]), values = [1, 2, 3]
Output: tree_to_list(...) == []

Constraints

  • 0 <= number of nodes <= 10**4, 0 <= len(values) <= 10**4, keys are distinct integers
  • Removing every key leaves an empty tree, which tree_to_list reports as [].

Goals

  • Apply deletion repeatedly, feeding each result into the next call
  • Survive keys that are absent and keys that empty the tree
  • Keep the search order correct after every single removal
Starting Python…