?? 10.cpp
字號:
/*求二叉樹的深度(后序遍歷)*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define null 0
typedef struct node
{ char data;
struct node *lchild;
struct node *rchild;
}* bitree;
int CreateBiTree(bitree &t) //按先序次序構造二叉樹
{char ch=' ';
scanf("%c",&ch);
if(ch==' ')t=null;
else{
if(!(t=(node *)malloc(sizeof(node))))return 0;
t->data=ch; //建立根節點
CreateBiTree(t->lchild);//建立左子樹
CreateBiTree(t->rchild);//建立右子樹
}
return 1;
}
int depth(bitree t)
{
int dep,dep1,dep2; //分別為:根,左,右子樹的深度
if(!t) //如果樹空
{
dep=0; //b的深度就是0
}
else
{
dep1=depth(t->lchild);
dep2=depth(t->rchild);
dep=1+(dep1>dep2?dep1:dep2);
}
return dep;
}
void main()
{bitree t=null;
int deep;
printf("輸入二叉樹中結點的值:");
CreateBiTree(t);
deep=depth(t);
printf("二叉樹深度為:");
printf("%d",deep);
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -