递归函数:自己调用自己的函数式递归函数。定义递归函数时,为了避免不断的重复循环不停止,应当引入判定条件。
以一个二叉树遍历例子来讲解:
二叉树的图为:
二叉树遍历的代码为:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct BiNode
{
char ch;
struct BiNode *lchild;
struct BiNode *rchild;
};
//二叉树递归遍历
void recursion(struct BiNode *root)
{
if (NULL==root)
{
return;
}
//后序遍历
//递归遍历左子树
recursion(root->lchild);
//递归遍历右子树
recursion(root->rchild);
printf("%c", root->ch);
}
void test()
{
struct BiNode nodeA = { 'A', NULL, NULL };
struct BiNode nodeB = { 'B', NULL, NULL };
struct BiNode nodeC =