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

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

數據結構實驗之二叉樹三:統計葉子數

2019-11-11 02:19:55
字體:
來源:轉載
供稿:網友

sdut原題鏈接

數據結構實驗之二叉樹三:統計葉子數 Time Limit: 1000MS Memory Limit: 65536KB

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

Input 連續輸入多組數據,每組數據輸入一個長度小于50個字符的字符串。

Output 輸出二叉樹的葉子結點個數。

Example Input abc,,de,g,,f,,,

Example Output 3

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;char s[104];int flag, y1;BinTree *root;BinTree * creat()//建樹{ 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 leave(BinTree *root)//計算葉子數目{ if(root)///判斷root是否為NULL { if(root->left == NULL && root->right == NULL) { y1++; } leave(root->left); leave(root->right); }}int main(){ while(scanf("%s", s) != EOF) { flag = y1 = 0; root = creat();//調用建樹函數 leave(root);//調用計算葉子數目函數 printf("%d/n", y1); } return 0;}/***************************************************User name: Result: AcceptedTake time: 0msTake Memory: 112KBSubmit time: 2017-02-07 11:46:51****************************************************/

以下為runtime error代碼

#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct node{ char date; struct node *left; struct node *right;}BinTree;char s[54];int flag, y1;BinTree *root;BinTree * creat()//建樹{ 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 leave(BinTree *root)//計算葉子數目{ if(!root->left && !root->right){ y1++; }}int main(){ while(scanf("%s", s) != EOF) { flag = y1 = 0; root = creat();//調用建樹函數 leave(root);//調用計算葉子數目函數 printf("%d/n", y1); } return 0;}/***************************************************User name: LEOResult: Runtime ErrorTake time: 0msTake Memory: 0KBSubmit time: 2017-02-07 11:31:15****************************************************/

runtime error cause: 1 在計算葉子數目函數中沒有判斷root是否為NULL;


上一篇:最少攔截系統

下一篇:【Bzoj1008】越獄

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邳州市| 同心县| 石渠县| 杭州市| 浦北县| 宜黄县| 海原县| 嘉兴市| 三台县| 高密市| 呼和浩特市| 阿鲁科尔沁旗| 凉城县| 中阳县| 盐山县| 静海县| 江达县| 白银市| 屏东市| 苍溪县| 桃江县| 洛阳市| 海门市| 盐池县| 宿松县| 宜川县| 扶绥县| 锡林郭勒盟| 景谷| 夏津县| 通江县| 荃湾区| 凌源市| 饶河县| 昌邑市| 凤台县| 德钦县| 淳安县| 绥江县| 蕉岭县| 鸡泽县|