国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇平县| 社旗县| 连城县| 烟台市| 靖远县| 都兰县| 台安县| 瑞昌市| 昆山市| 铅山县| 怀化市| 夏邑县| 龙游县| 本溪市| 峨眉山市| 罗甸县| 青铜峡市| 南溪县| 西乌珠穆沁旗| 丹凤县| 莱芜市| 焦作市| 周至县| 霍林郭勒市| 龙南县| 嘉禾县| 纳雍县| 察雅县| 平安县| 乌苏市| 个旧市| 潮安县| 邹城市| 德化县| 虞城县| 积石山| 宜昌市| 肥东县| 龙山县| 广汉市| 民权县|