该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
重新发一下、
#include "stdafx.h"
#include
using namespace std;
class stdTree;
stdTree *tr[16];
void loadtree(int);
int main()
{
int n;
cin>>n;
tr[1]=new stdTree();
loadtree(n);
int depth;
depth=tr[1]->scanheight(true);
cout<
scanwidth(depth)<cin.get();cin.get();
return 0;
}
void loadtree(int n)
{
int x=0,y=0;
for (int i=0;i
{
cin>>x>>y;
tr[x]=new stdTree();tr[y]=new stdTree();
tr[i+1]->lt=tr[x];
tr[i+1]->rt=tr[y];
}
}
class stdTree
{
public :
stdTree* lt;
stdTree* rt;
int intindex;
stdTree()
{
intindex=0;
lt=NULL;rt=NULL;
}
~stdTree()
{
if (lt!=NULL)delete lt;
if(rt!=NULL)delete rt;
}
int scanwidth(int depth)
{
if(depth==0)return (intindex!=0);
if(lt!=NULL)return lt->scanwidth(depth-1);
if(rt!=NULL)return rt->scanwidth(depth-1);
}
int scanheight(bool ifroot)
{
static int currentdepth;
if (ifroot)currentdepth=1;
if(intindex)
{
currentdepth++;
if(lt!=NULL)return lt->scanheight(false);
if(rt!=NULL)return rt->scanheight(false);
}
return currentdepth;
}
};