Arista Networks Interview Question
Software EngineersCountry: India
Interview Type: Written Test
struct node
{
struct node* left;
struct node* right;
int data;
};
struct node* array_to_tree(int array[], int start, int end);
void main()
{
struct node* root = NULL;
int array[]={2,4,6,8,9};
int len = sizeof(array)/sizeof(array[0]);
root = array_to_tree(array,0,len-1);
printf("root:%d\n",root->data);
printf("root->left:%d\n",root->left->data);
printf("root->right:%d\n",root->right->data);
printf("root->left->right:%d\n",root->left->right->data);
printf("root->right->right:%d\n",root->right->right->data);
}
struct node* array_to_tree(int array[], int start, int end)
{
int mid;
if(start>end)
{
return NULL;
}
else
{
mid= (start+end)/2;
struct node* root = (struct node*)malloc (sizeof(struct node));
root->data = array[mid];
root->left = array_to_tree(array,start,mid-1);
root->right = array_to_tree(array,mid+1, end);
return (root);
}
}
struct node
{
struct node* left;
struct node* right;
int data;
};
struct node* array_to_tree(int array[], int start, int end);
void main()
{
struct node* root = NULL;
int array[]={2,4,6,8,9};
int len = sizeof(array)/sizeof(array[0]);
root = array_to_tree(array,0,len-1);
printf("root:%d\n",root->data);
printf("root->left:%d\n",root->left->data);
printf("root->right:%d\n",root->right->data);
printf("root->left->right:%d\n",root->left->right->data);
printf("root->right->right:%d\n",root->right->right->data);
}
struct node* array_to_tree(int array[], int start, int end)
{
int mid;
if(start>end)
{
return NULL;
}
else
{
mid= (start+end)/2;
struct node* root = (struct node*)malloc (sizeof(struct node));
root->data = array[mid];
root->left = array_to_tree(array,start,mid-1);
root->right = array_to_tree(array,mid+1, end);
return (root);
}
}
c# implementation.
- zr.roman January 16, 2016