POJ 3437 Tree Grafting
//思路: 二叉树中X节点的高 = 二叉树中X的父节点的高 + X是第几个儿子
#include <iostream>
#include<string>
using namespace std;
string s;
int i,n=0,height1,height2;
void work(int level1,int level2){
int tempson=0;
while (s[i]=='d'){
i++; tempson++; //tempson 代表第几个 儿子
work(level1+1,level2+tempson);
}
height1=level1>height1?level1:height1;
height2=level2>height2?level2:height2;
i++;
}
int main()
{
while (cin>>s && s!="#"){
i=height1=height2=0;
work(0,0);
cout<<"Tree "<<++n<<": "<<height1<<" => "<<height2<<endl;
}
return 0;
}
浙公网安备 33010602011771号