Gamified challenge · In-order traversal
tree is a binary search tree of locker numbers in level order with null for a missing spot (TreeNode in code): every locker in a node's left subtree has a smaller number, every locker in its right subtree a bigger one. All numbers are different.
Write kthSmallest(tree, k) (Python: kth_smallest(tree, k)) that returns the number on the k-th smallest locker, counting from 1. k is always between 1 and the number of lockers.
For [5, 3, 6, 2, 4, null, null, 1] and k = 3, the lockers in order are 1, 2, 3, 4, 5, 6, so the answer is 3.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google