Tree Grafting(POJ 3437)

//思路:   二叉树中X节点的高  =  二叉树中X的父节点的高  +   X是第几个儿子

[cpp]  view plain  copy
  1. #include <iostream>    
  2. #include<string>  
  3. using namespace std;    
  4. string s;    
  5. int i,n=0,height1,height2;    
  6.   
  7. void work(int level1,int level2){    
  8.     int tempson=0;    
  9.     while (s[i]=='d'){    
  10.         i++; tempson++;  //tempson 代表第几个 儿子  
  11.         work(level1+1,level2+tempson);    
  12.     }    
  13.     height1=level1>height1?level1:height1;    
  14.     height2=level2>height2?level2:height2;    
  15.     i++;    
  16. }    
  17.   
  18. int main()    
  19. {    
  20.     while (cin>>s && s!="#"){    
  21.         i=height1=height2=0;    
  22.         work(0,0);    
  23.         cout<<"Tree "<<++n<<": "<<height1<<" => "<<height2<<endl;    
  24.     }    
  25.     return 0;    
  26. }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值