BJFUOJ:基于二叉链表的二叉树最大宽度的计算
#include<bits/stdc++.h>
using namespace std;
int maxWide = -1;
typedef struct BiTnode{
char data;
int deepth;
struct BiTnode *lchile,*rchild;
}BiTnode,*BiTree;
void CreatTree(BiTree &t,char s[],int &i,int d){
if(s[i] == '0'){
t = NULL;
}else{
t = new BiTnode;
t->deepth = d;
t->data = s[i];
CreatTree(t->lchile,s,++i,d+1);
CreatTree(t->rchild,s,++i,d+1);
}
}
void FindWide(BiTree &t,int wide []){
if(t){
wide[t->deepth]++;
maxWide = maxWide > wide[t->deepth] ? maxWide : wide[t->deepth];
FindWide(t->lchile,wide);
FindWide(t->rchild,wide);
}
}
int main(){
while(1){
maxWide = -1;
char s[100];
cin>>s;
if(s[0] == '0') break;
int wide[100];
memset(wide,0,sizeof(wide));
BiTree t;
int i = 0;
CreatTree(t,s,i,1);
FindWide(t,wide);
cout<<maxWide<<endl;
}
return 0;
}