题目是求二叉树的最大宽度,要求处理多组数据到文件尾,代码在Dev上是能运行的,但是在pta上判定段错误,我查过了,应该是多组数据的处理这里有问题,请问应该要怎么实现?
#include<iostream>
using namespace std;
struct BNode{
char data;
BNode *LChild;
BNode *RChild;
};
void Creat_BT(BNode*& p)
{
char ch;
cin>>ch;
if(ch=='*')
p=NULL;
else{
p=new BNode;
p->data=ch;
Creat_BT(p->LChild);
Creat_BT(p->RChild);
}
}
int FindDepth(BNode *p)
{
if(p==NULL)
return 0;
else{
int m=FindDepth(p->LChild);
int n=FindDepth(p->RChild);
return m>n?(m+1):(n+1);
}
}
int FindWidth(BNode *p,int level)
{
if(p==NULL) return 0;
else
{
if(level==1)
return 1;
else
return FindWidth(p->LChild,level-1)+FindWidth(p->RChild,level-1);
}
}
int Width(BNode *p)
{
int max=0;
int width=0;
int h=FindDepth(p);
for(int i=0;i<=h;i++)
{
width=FindWidth(p,i);
if(max<width)
max=width;
}
return max;
}
int main()
{
while(!cin.eof())
{
BNode *root;
Creat_BT(root);
cout<<"maxWidth: "<<Width(root)<<endl;
}
return 0;
}
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。