简体   繁体   中英

Post-order/Pre-order Traversal of Binary Tree

I have a pre-order traversal function that looks like this:

void listInPreOrder(node* hd){
if(hd != NULL) {
        printf("%d, ", hd->value);
        listInPreOrder(hd->left);
        listInPreOrder(hd->right);
    }
}

That in fact works, but I thought that making it post order would be as simple as this

void listInPostOrder(node* hd){
if(hd != NULL) {
        listInPreOrder(hd->left);
        listInPreOrder(hd->right);
        printf("%d, ", hd->value);
    }
}

But unfortunately it does not work so well. I'm wondering how to fix this, maybe I'm doing something simple wrong. Or maybe it's completely wrong.

How about you change this:

void listInPostOrder(node* hd){
if(hd != NULL) {
        listInPreOrder(hd->left);  // PRE order ???
        listInPreOrder(hd->right); // PRE order ???
        printf("%d, ", hd->value);
    }
}

to this:

void listInPostOrder(node* hd){
if(hd != NULL) {
        listInPostOrder(hd->left);
        listInPostOrder(hd->right);
        printf("%d, ", hd->value);
    }
}

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