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

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

數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)之二叉樹三:統(tǒng)計(jì)葉子數(shù)

2019-11-08 19:36:21
字體:
供稿:網(wǎng)友

PRoblem Description

已知二叉樹的一個按先序遍歷輸入的字符序列,如abc,,de,g,,f,,, (其中,表示空結(jié)點(diǎn))。請建立二叉樹并求二叉樹的葉子結(jié)點(diǎn)個數(shù)。 Input

連續(xù)輸入多組數(shù)據(jù),每組數(shù)據(jù)輸入一個長度小于50個字符的字符串。 Output

輸出二叉樹的葉子結(jié)點(diǎn)個數(shù)。 Example Input

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

3 Hint

Author

xam

#include<stdio.h>#include<stdlib.h>#include<string.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;};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);//遞歸把左子樹的葉子數(shù)跟右子樹的葉子數(shù)加起來}int main(){ while(scanf("%s", a) != EOF) { top = -1; struct node *root; root = creat(); printf("%d/n", yezi(root)); } return 0;}
上一篇:LeetCode || 001 Two Sum

下一篇:枚舉

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 松潘县| 乌拉特后旗| 乌兰浩特市| 云林县| 嵊泗县| 雷山县| 尉氏县| 梓潼县| 大悟县| 中山市| 连山| 凌源市| 古蔺县| 武邑县| 赞皇县| 大埔县| 满城县| 墨江| 西贡区| 萨嘎县| 闽侯县| 望都县| 涪陵区| 潢川县| 宜城市| 荔波县| 永平县| 什邡市| 清远市| 宝鸡市| 邯郸市| 宾阳县| 华池县| 天长市| 鹿泉市| 平和县| 昔阳县| 舟山市| 叙永县| 兴宁市| 招远市|