ORIGINAL CODING QUESTION
k-th smallest badge in a search tree
Badge numbers are stored in a valid strict binary search tree: everything in a left subtree is smaller than the node and everything in a right subtree is larger. Return the k-th smallest badge number, counting from k = 1.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n triples (value,leftChild,rightChild). Node 0 is the root; a missing child is written as -1. Then k.
Constraints
1 ≤ n ≤ 200; -100 ≤ value ≤ 100; the triples form one connected binary tree and every child ID is larger than its parent ID. Values are distinct and satisfy strict search-tree ordering; 1 ≤ k ≤ n.
Example 1
Input
5 5 1 2 3 3 4 8 -1 -1 1 -1 -1 4 -1 -1 3
Output
4
In order the badges are 1, 3, 4, 5, 8; the third is 4.
Example 2
Input
4 1 -1 1 2 -1 2 3 -1 3 4 -1 -1 4
Output
4
A right-leaning chain 1, 2, 3, 4 has 4 as its fourth value.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
An in-order walk of a search tree visits values in increasing order.
Hint 2
An explicit stack replaces recursion: go left as far as possible, then visit, then turn right.
Hint 3
Stop as soon as the k-th visit happens.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →