topic link
Topic description
Input a string of binary trees and print them out with traversal preorder.
Input and output format
Input format:The first line is the number of nodes n of the binary tree. ( n \leq 26n≤26 )
In the next n lines, each letter is a node, and the last two letters are its left and right sons respectively.
Empty nodes are represented by *
Output format:preorder binary tree
Input and output example
Input example #1:
6 abc bdi cj* d** i** j**
Sample output #1:
abdicj
Ideas:
Simple pre-order traversal of binary tree, the key point is that I am a konjac who does not know all kinds of knowledge about character classes, so I have to roughly convert char to int...
Code:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 250 #define loop( i, a, b ) for( int i = a; i <= b; i++ ) using namespace std; int n, kong = '*' - '0'; int f[maxn], v[maxn], l[maxn], r[maxn]; void haha_tree() { char p, lc, rc; scanf( "%d", &n ); loop( i, 1, n ) { cin>>p>>lc>>rc; int llc, rrc, pp; pp = p - '0'; //Rough transformation. llc = lc - '0'; rrc = rc - '0'; if( llc != kong ) { f[llc] = pp; l[pp] = llc; } if( rrc != kong ) { f[rrc] = pp; r[pp] = rrc; } } } inline int find_root() { //Why do you force me to write a board? int aa = 'a' - '0'; // Still rough. int bb = 'z' - '0'; loop( i, aa, bb ) if( f[i] == 0 ) return i; } inline void xianxu( int i ) { if( i ) { chariot years; years = i + '0'; cost<<years; xianxu( l[i] ); xianxu( r[i] ); } } int main() { haha_tree(); int gen = find_root(); xianxu( gen ); return 0; }