Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.
void flatten(node *node, int a[]){
int pos = 0;
if(node){
flatten(node->pleft,a);
a[pos++] = node->data;
flatten(node->pright,a);
}
}
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
Title | # Comments | Views | Activity |
---|---|---|---|
undefined reference to `bswap_128' | 9 | 160 | |
Fast data scrub | 19 | 96 | |
how to bring to back always mid parent image form | 21 | 79 | |
JavaFX TableView not displaying correctly | 3 | 16 |
Join the community of 500,000 technology professionals and ask your questions.