# Elements In Two BSTs

Easy
0/40
Average time to solve is 15m
Contributed by

## Problem statement

You have been given two Binary Search Trees â€˜root1â€™ and â€˜root2â€™ of integers and you want to collect all the elements present in both the BSTs.

Your task is to print all the integers from both BSTs sorted in ascending order.

A binary search tree (BST) is a binary tree data structure which has the following properties.

``````â€¢ The left subtree of a node contains only nodes with data less than the nodeâ€™s data.
â€¢ The right subtree of a node contains only nodes with data greater than the nodeâ€™s data.
â€¢ Both the left and right subtrees must also be binary search trees.
``````

For Example:

``````For the given BSTs:
``````

``````The output will be: [2, 2, 5, 7, 7, 10, 10, 16, 16, 20, 20]
``````
Detailed explanation ( Input/output format, Notes, Images )
##### Sample Input 1:
``````1
15 10 20 8 12 16 25 -1 -1 -1 -1 -1 -1 -1 -1
10 -1 90 -1 -1
``````
##### Sample output 1:
``````8 10 10 12 15 16 20 25 90
``````
##### Explanation of Sample output 1:
``````The trees can be represented as follows:
``````

``````The nodes of both BST in increasing order are 8 10 10 12 15 16 20 25 90.
``````
##### Sample Input 2:
``````1
10 5 -1 -1 -1
20 -1 -1
``````
##### Sample output 2:
``````5 10 20
``````
Console