Interview Questions/Coding/BST Query Scanner

BST Query Scanner

Preview mode. Log in to edit, run, submit, and save progress.

Easy

BST Query Scanner

A data archive stores numbers inside a binary search tree. Values smaller than a node go to its left subtree, while values greater than or equal to a node go to its right subtree. You are given the order in which values are inserted into the tree, followed by a list of query values. For every query, return 1 if the value exists in the BST and 0 otherwise.

Examples

Example 1
Input:
5 3 7 2 4 6 8
4 9 5
Output:
1 0 1

Explanation: Values 4 and 5 are present in the tree, while 9 is not.

Approach hint

Insert the values into the BST using the normal BST rule.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp