下落的樹葉 (The Falling Leaves UVA - 699)
阿新 • • 發佈:2018-08-26
sum std name fall include ++ 源代碼 lock 水平
題目描述:
原題:https://vjudge.net/problem/UVA-699
題目思路:
1.依舊二叉樹的DFS
2.建樹過程中開個數組統計
//紫書源代碼WA
AC代碼:
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 const int maxn = 100 ; 6 int sum[maxn] ; 7 8 void buildtree(int val,int p) //建樹,p為樹根的水平位置 9 { 10 intvl,vr; 11 if(val == -1) return ; 12 sum[p] += val ; 13 cin >> vl ; 14 buildtree(vl,p - 1); 15 cin >> vr ; 16 buildtree(vr,p + 1); 17 } 18 19 int main(int argc, char *argv[]) 20 { 21 int k=0,v; 22 while(cin >> v && v != -1){23 memset(sum,0,sizeof(sum)) ; 24 int pos = maxn/2 ; 25 buildtree(v,pos) ; 26 int p = 0; 27 while(sum[p] == 0) p++ ; //最左邊的子葉 28 cout << "Case " << ++k << ":\n" << sum[p++]; 29 while(sum[p] != 0) { 30 cout << "" << sum[p]; 31 p++; 32 } 33 cout << "\n\n"; 34 } 35 return 0; 36 }
下落的樹葉 (The Falling Leaves UVA - 699)