1
votes

I try to write a function which return an array of integers containing the node values of the binary tree in preorder that is a node value must appear before the values of its left and right child.

  • if root is NULL, return NULL

  • for every node left child comes before right child

For example;

int *a = preorder(bt1);
for (i=0; i<3; i++)
    printf("%d ", a[i]);
>2_1_3_

Here is my work, but it doesn't work, where could be the problem in my code?

int* preorder(TreeNode *root) {
    int *a = malloc(sizeof(int)*50);
    int i=0;

    if(root == NULL)
        return NULL;
    else {
        if(root != NULL) {
            a[i] = root->val;
            i++;
            preorder(root->left);
            preorder(root->right);
            return a;
        }
    }
}
3
The allocation doesn't make sense. What exactly is unclear? - 2501
Each call of preorder is storing the result into a different array. That is clearly not what you want. You need to pass the array to each preorder call. - kaylum

3 Answers

3
votes

You have two issues in the code:

  1. You have to allocate the array for results once. Before calling preorder
  2. You have to keep i outside of preorder to allow it to change between calls

One example is the following code:

int *a = malloc(sizeof(int)*50);
int inx = 0;
preorder(bt1, a, &inx);



void preorder(TreeNode *root, int* a, int* inx) {
    if(root == NULL)
        return;
    else {
        if(root != NULL) {
            a[*inx] = root->val;
            *inx = *inx + 1;
            preorder(root->left, a, inx);
            preorder(root->right, a, inx);
        }
    }
}
0
votes

In each recursive call of this function you will allocate:

int *a = malloc(sizeof(int)*50);

You need to allocate space for array once and then use that same array. Same thing is for using i = 0. You need to use one counter.

You may want create array in main function, and then pass array as function argument. Or you could use global array, and access it that way. Same thing for counter variable.

Note: I don't see point of memory allocation in your example. You should better use static array if you're sure that tree won't have more then ARRAY SIZE nodes.

0
votes

With the wanted function signature of preorder() a solution is not possible. Therefore you need a helper function for the root == NULL case and a traversal function which a pointer to the current position in the array. It also returns a pointer to the next free slot in the array. A solution could look like:

#include <stdio.h>
#include <malloc.h>

struct TreeNode {
    int val;
    struct TreeNode* left, * right;
};

int tree_size(/*struct TreeNode* tree*/) { return 7; }

int* preorder_(struct TreeNode* tn, int* v) {
    *v++ = tn->val;
    if (tn->left) v = preorder_(tn->left, v);
    if (tn->right) v = preorder_(tn->right, v);

    return v;
}

int* preorder(struct TreeNode* tn) {
    if (tn) {
        int* v = malloc(tree_size(/*tn*/) * sizeof(int));
        preorder_(tn, v);
        return v;
    } else {
        return NULL;
    }
}

int main(void) {
    //    4
    //  2   5
    // 1 3 6 7
    struct TreeNode
        left = {2, &{1}, &{3]}, 
        right = {5, &{6}, &{7}}, 
        root = {4, &left, &right};
    int *v, i;

    v = preorder(&root);
    for (i = 0; i < tree_size(/*tn*/); i++) {
        printf("%d ", v[i]); // 4 2 1 3 5 6 7
    }
    free(v);

    return 0;
}

Live Demo