简体   繁体   中英

Why is it wrong to write RETURN ? ( postorder traversal )

void Postorder( IntPtr head ) {
    if ( head != NULL ) {
        return Postorder( head -> left ); // problem
        return Postorder( head -> right ); // problem
        printf( "%d\n", head -> data );
    } // if
}

Why is it wrong to write RETURN? I want to know.

I think it will find head-> left all the way to the bottom, if head -> left is NULL, it will go back to the above one layer and start looking for head -> right, and finally output

return means "I'm finished here, so don't execute any more code in this function. So when the code encounters the first return statement it returns from the function, and doesn't execute the second call to PostOrder or the printf statement.

Off the top of my head, I suspect that simply removing the word return from both of those statements will fix the problem:

void Postorder( IntPtr head ) {
    if ( head != NULL ) {
        Postorder( head -> left );
        Postorder( head -> right );
        printf( "%d\n", head -> data );
    } // if
}

You return before you printf .

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