資料結構實驗之二叉樹五:層序遍歷 (SDUT 3344)
阿新 • • 發佈:2018-11-19
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; }; char s[505]; int num; struct node *creat() { struct node *root; if(s[num ++] == ',') { root = NULL; } else { root = new node; root -> data = s[num - 1]; root -> lc = creat(); root -> rc = creat(); } return root; }; void level(struct node *root) { struct node *q[100]; int in = 0, out = 0; q[in++] = root; while(out < in) { if(q[out]) { printf("%c", q[out] -> data); q[in++] = q[out] -> lc; q[in++] = q[out] -> rc; } out++; } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%s",s); num = 0; struct node *root; root = creat(); level(root); printf("\n"); } return 0; }