📄 求二叉树的深度.cpp
字号:
/* Note:Your choice is C IDE */
#include"stdio.h"
#include"iostream.h"
#include"stdlib.h"
#define ok 1
#define overflow -1
#define null 0
typedef struct bitnode{
char data;
struct bitnode *lchild,*rchild;
}bitnode, *bitree;
char createbitree(bitree &t){
char ch;
cin>>ch;
if(ch=='#') t=null;
else{
if(!(t=(bitnode*)malloc(sizeof(bitnode)))) exit(overflow);
t->data=ch; //生成根结点
createbitree(t->lchild); //生成左子树
createbitree(t->rchild); //生成右子树
}
return ok;
}
int bintreedepth(bitree t){
int n,l,r;
if(t==null) return 0;
else{
l=bintreedepth(t->lchild);
r=bintreedepth(t->rchild);
return((l>r?l:r)+1);
}
}
void main(){
int n,m;
bitree p;
cout<<"Please enter the node:";
createbitree(p);
bintreedepth(p);
cout<<"The depths of this binary tree are: ";
cout<<bintreedepth(p);
cout<<endl;
cout<<"If you want to continue,please enter the button 1,if not,enter the button 2;"<<endl;
cout<<"m=";
cin>>m;
while(m==1){
cout<<"*******************************************************************************"<<endl;
cout<<"Please enter the node:";
createbitree(p);
bintreedepth(p);
cout<<"The depths of this binary tree are: ";
cout<<bintreedepth(p);
cout<<endl;
cout<<"If you want to continue,please enter the button 1,if not,enter the button 2;"<<endl;
cout<<"m=";
cin>>m;
cout<<"********************************************************************************"<<endl;
}
if(m==2) cout<<"over"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -