Create BST

3.7

3 votes
Very-Easy
Problem

Create a Binary Search Tree from list A containing N elements. Insert elements in the same order as given. Print the pre-order traversal of the subtree with root node data equal to Q (inclusive of Q), separating each element by a space.

Input:
First line contains a single integer N – number of elements.
Second line contains N space-separated integers.
Third line contains a single integer Q – the element whose subtree is to be printed in pre-order form.

Output:
Print K space-separated integers – where K is the number of elements in the subtree of Q (inclusive of Q)

Constraints:

1N103
109A[i]109

Sample Input
4
2 1 3 4
3
Sample Output
3
4
Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?