BETA

Difficulty Level: Medium

Submissions: 1556 Accuracy:

30.21%

Print leaf nodes from preorder traversal of BST
         

Given a preorder traversal of a BST, print the leaf nodes of the tree without building the tree.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the no of nodes of the BST. Then in the next line are N space separated values denoting the preorder traversal of the binary tree.

Output:
For each test case in a new line print the leaf nodes separated by space of the BST in sorted order.

Constraints:
1<=T<=100
1<=N<=100

Example:
Input:

2
5
890 325 290 530 965 
3
3 2 4

Output:
290 530 965
2 4

** For More Input/Output Examples Use 'Expected Output' option **

Set Default Code

					   
It is recommended to 'Compile & Test' your code before clicking 'Submit'!

Compilation/Execution Result:

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.