11þ-
来自「1、猴子选大王 2、约瑟夫环 3、迷宫求解 4、回文游戏 5、地图四染色」· 代码 · 共 60 行
TXT
60 行
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
#define OVERFLOW 0
#define STACKSIZE 50
#define ADD 20
typedef struct CSNode
{
char data;
struct CSNode *firstchild;
struct CSNode *nextsibling;
}CSNode,* CSTree;
void CreateCSTree(CSTree &T) //先序扩展序列建立树的递归算法
{
char ch;
scanf("%c",&ch);
if (ch=='.')
{
T=NULL;
}
else
{
if(!(T=(CSNode*)malloc(sizeof(CSNode))))
{
exit(OVERFLOW);
}
T->data=ch;
CreateCSTree(T->firstchild);
CreateCSTree(T->nextsibling);
}
}
int Depth(CSTree T)//递归
{ int dep1,dep2;
if(T==NULL) return(0);
else
{ dep1=Depth(T->firstchild);//求第一个孩子子树的深度
dep2=Depth(T->nextsibling);//求下一个兄弟子树的深度
if(dep1+1>dep2) return(dep1+1);//树的深度为"max(左深+1,右深)"
else return(dep2);
}
}
void main()
{ CSTree T=NULL;
printf("请输孩子兄弟法输入树,当子树为空时,用.来代替:\n");
CreateCSTree(T);
if(T){
printf("此树的深度为:");
printf("%d",Depth(T));
printf("\n");
}
else printf("树为空!\n");
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?