简体   繁体   中英

Runtime error in PC2 but works in Dev-C++

I am trying to do the assignment. My code is successful for the example input and output, but when it comes to PC2 test, it has runtime error and I don't know why. I have to an inorder traversal binary tree given the preorder and postorder. Here's my code:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node Node;
struct Node { 
    int data; 
    Node *left; 
    Node *right; 
}; 

Node *getNewNode(int data) { 
    Node *newNode = (Node*)malloc(sizeof(Node)); 
    newNode->data = data; 
    newNode->right = newNode->left = NULL; 
    return newNode; 
}

Node *buildTreeRecur (int pre[], int post[], int* preIndex, int low, int high, int size) {
    if (*preIndex >= size || low > high)
        return NULL;

    Node *root = getNewNode(pre[*preIndex]);
    *preIndex++;

    if (low == high)
        return root;

    int i;
    for (i=low; i<=high; i++) {
        if (pre[*preIndex] == post[i])
            break;
    }

    if (i<=high) {
        root->left = buildTreeRecur(pre, post, preIndex, low, i, size);
        root->right = buildTreeRecur(pre, post, preIndex, i+1, high, size);
    }

    return root;
}

Node *buildTree (int pre[], int post[], int size) {
    int preIndex = 0;
    return buildTreeRecur(pre, post, &preIndex, 0, size-1, size);
}

void printInorder (Node *node) {
    if (node == NULL)
        return;
    printInorder(node->left);
    printf("%d ", node->data);
    printInorder(node->right);

}

int main(int argc, char *argv[]) {
    int numCase, size, i;
    printf("Enter number of case: ");
    scanf("%d", &numCase);
    printf("Enter the size of tree: ");
    scanf("%d", &size);
    int pre[size], post[size];

    printf("Enter the preorder tree:\n");
    for(i=0; i<size; i++) {
        scanf("%d", &pre[i]);
    }

    printf("Enter the postorder tree:\n");
    for(i=0; i<size; i++) {
        scanf("%d", &post[i]);
    }

    Node *root = buildTree(pre, post, size);

    printf("The inorder traversal of the full binary tree: \n");
    printInorder(root);

    return 0;
}

The input case that success is the following.

1
9
1 2 4 8 9 5 3 6 7
8 9 4 5 2 6 7 3 1

The output is:

8 4 9 2 5 1 6 3 7

But I don't know what case I missed which cause runtime error..

I know there's debugger in Dev-C++ which I'm using right now, but I don't know how to use it too.

Here:

*preIndex++;

you advance the pointer. (You then dereference it an throw away the value. When you activate warnings, the compiler shoud tell you about the unused value. Unfortunately, warnings are off by default in many compilers. Switch them on.)

Advancing the pointer is useful when the pointer points into an array, but preIndex points to a single int. After advancing it, it points to somewhere undefined. That leads to undefined behaviour, which means that the program might crash or not. All bets are off from now on.

You want to increment the integer index that preIndex points to:

(*preIndex)++;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM