[Cpp]
/** Topic: Create a binary tree in sequence and calculate the sum of leaf nodes on the same vertical line.
* Simulate the process of creating a binary tree in the first order, and store values in an array. The array subscript represents the vertical coordinate solution.
*/
# Include <cstdio> www.2cto.com
# Include <cstring>
Using namespace std;
Struct Node {
Int v, pos;
Node * lc, * rc;
};
# Define MID 81
Int a [MID * 2];
Int pre_create_tree (int pos ){
Int v;
Scanf ("% d", & v );
If (-1 = v) return 1;
A [MID + pos] + = v;
Pre_create_tree (pos-1 );
Pre_create_tree (pos + 1 );
Return 0;
}
Int main (int argc, char const * argv []) {
# Ifndef ONLINE_JUDGE
Freopen ("test. in", "r", stdin );
# Endif
Int flag, I (1 );
While (true ){
Flag = 0;
Memset (a, 0, sizeof ());
If (pre_create_tree (0) break;
Printf ("Case % d: \ n", I ++ );
For (int I = 0; I <MID * 2; I ++ ){
If (! A [I]) continue;
If (! Flag) {printf ("% d", a [I]); flag = 1 ;}
Else printf ("% d", a [I]);
} Www.2cto.com
Printf ("\ n ");
}
Return 0;
}