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 |
---|---|---|---|
Eclipse. How to move up/down source folders | 4 | 77 | |
An API detour question | 7 | 79 | |
Using popen() and gunzip() to open file in HTTPServer | 6 | 49 | |
Online file editor, manager | 6 | 62 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
15 Experts available now in Live!