5
1 2 3 4 5
3 1 2 4 5
#include <stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* newnode(int data){
struct node* root=(struct node*)malloc(sizeof(struct node));
root->data=data;
root->left=NULL;
root->right=NULL;
};
struct node* func(int arr[], int start, int end){
if(start>end){return NULL;}
int mid=(start+end)/2;
struct node* root=newnode(arr[mid]);
root->left=func(arr,start,mid-1);
root->right=func(arr,mid+1,end);
return root;
}
void preorder(struct node* root){
if(root==NULL){return ;}
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
struct node* root=func(arr,0,n-1);
preorder(root);
return 0;
}