国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

數(shù)據(jù)結(jié)構(gòu)實驗之二叉樹五:層序遍歷(二叉樹+隊列)

2019-11-10 22:53:29
字體:
供稿:網(wǎng)友

sdut原題鏈接

數(shù)據(jù)結(jié)構(gòu)實驗之二叉樹五:層序遍歷 Time Limit: 1000MS Memory Limit: 65536KB

PRoblem Description 已知一個按先序輸入的字符序列,如abd,,eg,,,cf,,,(其中,表示空結(jié)點)。請建立二叉樹并求二叉樹的層次遍歷序列。

Input 輸入數(shù)據(jù)有多行,第一行是一個整數(shù)t (t<1000),代表有t行測試數(shù)據(jù)。每行是一個長度小于50個字符的字符串。

Output 輸出二叉樹的層次遍歷序列。

Example Input 2 abd,,eg,,,cf,,, xnl,,i,,u,,

Example Output abcdefg xnuli

Hint

Author xam

以下為accepted代碼

#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct node{ char date; struct node *left; struct node *right;} BinTree;BinTree *root, *link[54];char s[54];int flag;BinTree * creat()//建立二叉樹函數(shù){ BinTree * root; if(s[flag++] == ',') root = NULL; else { root = (BinTree *)malloc(sizeof(BinTree)); root->date = s[flag-1]; root->left = creat(); root->right = creat(); } return root;}void ans(BinTree *root)//二叉樹的層序遍歷函數(shù){ if(root) { int i = 0, j = 0; link[j++] = root; while(i < j) { if(link[i]) { link[j++] = link[i]->left; link[j++] = link[i]->right; printf("%c", link[i]->date); } i++; } }}int main(){ int T; scanf("%d", &T); while(T--) { flag = 0; scanf("%s", s); root = creat();//調(diào)用建立二叉樹函數(shù) ans(root);//調(diào)用二叉樹的層序遍歷函數(shù) printf("/n"); } return 0;}/***************************************************User name: jk160630Result: AcceptedTake time: 0msTake Memory: 120KBSubmit time: 2017-02-07 21:36:35****************************************************/
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长寿区| 苏尼特右旗| 乌审旗| 和林格尔县| 瓮安县| 察哈| 闵行区| 九龙县| 平顺县| 乐昌市| 增城市| 瑞丽市| 清新县| 柳州市| 武冈市| 海口市| 连城县| 兴义市| 泰宁县| 宜川县| 青冈县| 建昌县| 昌江| 互助| 宿松县| 沂南县| 钟山县| 大渡口区| 城固县| 延庆县| 惠东县| 永州市| 平安县| 镇远县| 杭州市| 勐海县| 漠河县| 保靖县| 沾益县| 乌鲁木齐市| 湖南省|