如何将值1传递给main函数中的if语句以及返回函数如何与递归调用一起使用?返回函数如何返回If语句的值?
#include <stdio.h>
#include <stdlib.h>
int identicalTrees(struct node* a, struct node* b)
{
if (a==NULL && b==NULL)
return 1;
if (a!=NULL && b!=NULL)
{
return
(
a->data == b->data &&
identicalTrees(a->left, b->left) &&
identicalTrees(a->right, b->right)
);
}
return 0;
}
int main()
{
if(identicalTrees(root1, root2))
printf("Both tree are identical.");
else
printf("Trees are not identical.");
getchar();
return 0;
}
什么“返回功能”? 'return'不是一个函数,它是一个声明。并且它返回一个值给调用者,因为语言定义是这样说的。 –
有几个很棒的视频解释了递归和堆栈如何在YouTube上的Computerphile频道上工作。你应该全部看着他们。 https://www.youtube.com/watch?v=Mv9NEXX1VHc,https://www.youtube.com/watch?v=TrfcJCulsF4,https://www.youtube.com/watch?v=7t_pTlH9HwA,https: //www.youtube.com/watch?v=7ha78yWRDlE,https://www.youtube.com/watch?v=S_NXz7I5dQc – SpiderPig
您一定要阅读[call stack](https://en.wikipedia。组织/维基/ CALL_STACK)。 –