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

首頁 > 學院 > 開發設計 > 正文

數據結構實驗之二叉樹七:葉子問題

2019-11-08 18:47:01
字體:
來源:轉載
供稿:網友

PRoblem Description

已知一個按先序輸入的字符序列,如abd,,eg,,,cf,,,(其中,表示空結點)。請建立該二叉樹并按從上到下從左到右的順序輸出該二叉樹的所有葉子結點。 Input

輸入數據有多行,每一行是一個長度小于50個字符的字符串。 Output

按從上到下從左到右的順序輸出二叉樹的葉子結點。 Example Input

abd,,eg,,,cf,,, xnl,,i,,u,, Example Output

dfg uli Hint

Author

xam

這個題就是用層序遍歷的方法到達最后一層輸出

#include<stdio.h>#include<string.h>#include<stdlib.h>char a[55];int top;struct node{ int data; struct node *l, *r;};struct node *creat(){ struct node *root; top++; if(a[top] == ',') root = NULL; else { root = (struct node*) malloc(sizeof(struct node)); root -> data = a[top]; root -> l = creat(); root -> r = creat(); } return root;};void yezi(struct node *root){ int in = 0, out = 0; struct node *p[100]; p[in++] = root; while(in > out) { if(p[out]) { if(p[out] -> l == NULL && p[out] -> r == NULL)//判斷是不是葉子 printf("%c", p[out] -> data); p[in++] = p[out] -> l; p[in++] = p[out] -> r; } out++; }}int main(){ while(scanf("%s", a) != EOF) { top = -1; struct node *root; root = creat(); yezi(root); printf("/n"); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新源县| 永和县| 米脂县| 隆安县| 纳雍县| 毕节市| 岳普湖县| 德州市| 犍为县| 大邑县| 榕江县| 祥云县| 屯昌县| 法库县| 吴忠市| 湘潭市| 健康| 孝昌县| 武清区| 衡东县| 当雄县| 贵州省| 望谟县| 横峰县| 大竹县| 德庆县| 平邑县| 济宁市| 罗源县| 大宁县| 大同县| 禄丰县| 海安县| 巴青县| 灵丘县| 灵丘县| 岑巩县| 杭州市| 桃园县| 麻栗坡县| 商水县|