?? 樹的高度、葉子節點數(自己輸數據).c
字號:
#include<stdlib.h>
#include<stdio.h>
#define null 0
static int NUM=0;
static int DEEP=0;
static int TEMP=0;
typedef struct BinNode
{
char data;
struct BinNode *fch,*nsb;
} BinNode,*Bintree;
void crt_pre(Bintree *t)
{
char ch;
scanf("%c",&ch);
if (ch=='.')
*t=null;
else
{
*t=(BinNode *)malloc(sizeof(BinNode));
(*t)->data=ch;
crt_pre(&(*t)->fch);
crt_pre(&(*t)->nsb);
}
}
void preorder( BinNode *t,int temp)
{ int top=temp;
if(t!=null)
{
TEMP=temp+1;
if(t->fch==null)
{
NUM++;
if(TEMP>DEEP)DEEP=TEMP;
}
printf("%c",t->data);
preorder(t->fch,top+1);
preorder(t->nsb,top);
}
}
void main()
{
Bintree bt;
printf("輸入數據建立樹:");
crt_pre(&bt);
preorder(bt,0);
printf("\n");
printf("高度為:%d\n",DEEP);
printf("葉子數為:%d",NUM);
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -