eolymp
bolt
Try our new interface for solving problems
Problems

Pre-Order Traversal of a Tree

Pre-Order Traversal of a Tree

Time limit 1 second
Memory limit 128 MiB

Given an array of integers. Create a Binary Search Tree from these numbers. If the inserted value equals to the current node, insert it to the right subtree.

Write method PreOrder that makes Pre-Order traversal of a tree. In this traversal method the root node is visited first, then the left subtree and finally the right subtree.

prb7463.gif

Write the code according to the next interface:

class TreeNode

{

public:

int val;

TreeNode *left;

TreeNode *right;

TreeNode(int x) : val(x), left(NULL), right(NULL) {}

};

class Tree

{

public:

TreeNode *head;

Tree() : head(NULL) {};

void Insert(int val); // Insert number val to Binary Search Tree

void PreOrder(void); // Print the vertices of a tree according to pre-order traversal

};

You can create (use) additional methods if needed.

Input data

The first line contains number n (1n100). The second line contains n integers.

Output data

Create the Binary Search Tree from input data. Print the vertices of a tree according to pre-order traversal.

Examples

Input example #1
6
14 9 1 14 20 13
Output example #1
14 9 1 13 14 20 
Author Mykhailo Medvediev
Source C++