Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.
void flatten(node *node, int a[]){
int pos = 0;
if(node){
flatten(node->pleft,a);
a[pos++] = node->data;
flatten(node->pright,a);
}
}
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
void flatten(node *node, int a[]){
static int pos = 0;
if(node){
flatten(node->pleft,a);
a[pos++] = node->data;
flatten(node->pright,a);
}
}
void flatten(node *node, int a[]){
int pos = 0;
flattenHelper(node, a, &pos);
}
void flattenHelper(node *node, int a[], int *pos){
if(node){
flattenHelper(node->pleft,a, pos);
a[(*pos)++] = node->data;
flattenHelper(node->pright,a, pos);
}
}
void flatten(node *node, int a[], int pos){
int pos;
if(node == NULL)
return pos; // pos get the position of the last node
pos = flatten(node->pleft,a, pos); //go to left subtree until null
a[pos] = node->data; //write in the array at the position
pos = flatten(node->pright,a, pos); // go to right subtree
}
}
int flatten(node *node, int a[], int pos){
if(node == NULL)
return pos;
pos = flatten(node->pleft,a, pos);
a[pos] = node->data;
pos = flatten(node->pright,a, pos);
return pos;
}
int flatten(node *node, int a[], int pos){
if(node == NULL)
return pos;
pos = flatten(node->pleft,a, pos + 1); //a[0] would be lost, so starts in a[1]
a[pos] = node->data;
pos = flatten(node->pright,a, pos + 1);
return pos;
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.