繁体   English   中英

为什么写 RETURN 是错误的? (后序遍历)

[英]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
}

为什么写 RETURN 是错误的? 我想知道。

我想它会一直找到head->left一直到底部,如果head->left是NULL,它会go回到上面一层开始找head->right,最后是Z78E6221F6393D136DCE86681

return的意思是“我已经完成了,所以不要再执行这个 function 中的任何代码。所以当代码遇到第一个return语句时,它会从 function 返回,并且不执行对PostOrderprintf的第二次调用.

在我的脑海中,我怀疑只需从这两个语句中删除单词return就可以解决问题:

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

您在printf之前return

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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