求树的深度
可能编译时会有些语法小错误(比如分号,->,等),很容易就自己纠正了哦,思路绝对是完全正确的,所以用的话就自己试着改改吧,直接复制粘贴,就正确,岂不是太没写代码体验了,自己改改才印象更加深刻的呢(^▽^)~~~~;
#include<iostream>
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T){
char ch;
cin >> ch;
if(ch=='#') T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int Depth(BiTree T){
int m,n;
if(T==NULL) retue 0;
else{
m=Depth(T->lchild);
n=Depth(T->rchild);
if(m>n) return(m+1);
else{
return (n+1);
}
}
}
void main(){
BiTree tree;
cout<<"please input:\n";
CreateBiTree(tree);
cout<<"deepth is:"<<Depth(tree)<<endl;
}
更多精彩