Convert a BST to a sorted array · Compiler

Convert a BST to a sorted array

Convert the given binary search tree into an array in increasing key order. Find the index of a query key x in that array, counting the first position as index 0.

Input
Read n (0 <= n <= 100), then n rows of value left right. Rows give nodes in ID order from 1 to n. Each value is an integer in [-1000, 1000]. Each child link is a node ID or 0 for a missing child. For n > 0, the links form one binary tree rooted at ID 1. The tree is a BST with distinct keys. After the node rows read the query integer x in [-1000, 1000].
Output
On the first line print the sorted keys separated by spaces, or EMPTY for an empty array. On the second line print the zero-based index of x, or -1 if x is absent.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…