POJ 3437 Tree Grafting

来源:互联网 发布:台湾衰落知乎 编辑:程序博客网 时间:2024/05/29 07:35

//思路:   二叉树中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;  }  


0 0
原创粉丝点击