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

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

數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)之二叉樹的建立與遍歷

2019-11-08 18:41:10
字體:
供稿:網(wǎng)友

PRoblem Description

已知一個(gè)按先序序列輸入的字符序列,如abc,,de,g,,f,,,(其中逗號(hào)表示空節(jié)點(diǎn))。請(qǐng)建立二叉樹并按中序和后序方式遍歷二叉樹,最后求出葉子節(jié)點(diǎn)個(gè)數(shù)和二叉樹深度。

Input

輸入一個(gè)長(zhǎng)度小于50個(gè)字符的字符串。 Output

輸出共有4行: 第1行輸出中序遍歷序列; 第2行輸出后序遍歷序列; 第3行輸出葉子節(jié)點(diǎn)個(gè)數(shù); 第4行輸出二叉樹深度。 Example Input

abc,,de,g,,f,,, Example Output

cbegdfa cgefdba 3 5 Hint

Author

ma6174

#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 = (struct node*) malloc(sizeof(struct node)); root -> data = a[top]; root -> l = creat(); root -> r = creat(); } else root = NULL; return root;};void zhongxu(struct node *root){ if(root) { zhongxu(root -> l); printf("%c", root -> data); zhongxu(root -> r); }}void houxu(struct node *root){ if(root) { houxu(root -> l); houxu(root -> r); printf("%c", root -> data); }}int yezi(struct node *root){ if(root == NULL) return 0; if(root -> l == NULL && root -> r == NULL) return 1; else return yezi(root -> l) + yezi(root -> r);}int deep(struct node *root){ int d1, d2; if(root) { d1 = deep(root -> l) + 1; d2 = deep(root -> r) + 1; return d1 > d2? d1 : d2; } return 0;}int main(){ top = -1;//啊啊啊啊啊,掉了這一步浪費(fèi)了我好長(zhǎng)時(shí)間?。?! scanf("%s", a); struct node *root; root = creat(); zhongxu(root); printf("/n"); houxu(root); printf("/n"); printf("%d/n", yezi(root)); printf("%d/n", deep(root)); return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江津市| 商都县| 南丰县| 于田县| 措勤县| 芒康县| 麻城市| 彰化县| 德清县| 油尖旺区| 昭觉县| 北京市| 河西区| 偏关县| 唐河县| 高青县| 昌吉市| 林州市| 静乐县| 临沂市| 女性| 泾阳县| 洛阳市| 吉木萨尔县| 万源市| 黄浦区| 万盛区| 靖州| 惠东县| 西华县| 凌源市| 亳州市| 新河县| 虹口区| 诏安县| 屯留县| 山阴县| 洞头县| 凤城市| 宁化县| 龙江县|