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

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

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

2019-11-06 06:21:57
字體:
來源:轉載
供稿:網友

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

TimeLimit: 1000MS Memory Limit: 65536KB

SubmitStatistic

PRoblem Description

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

Input

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

Output

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

Example Input

abd,,eg,,,cf,,,

xnl,,i,,u,,

Example Output

dfg

uli

Hint

 

Author

 xam

 

#include<string.h>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<queue>#include<iostream>using namespace std;typedef struct node{   char data;   struct node*l;   struct node*r;}tree;tree* creat(char *&ss)//將一個一維數組轉換建立成為一個先序建立的樹{   tree*root = new tree;   if(*ss==',')   {      ss++;      return NULL;   }   root->data = *ss++;   root->l = creat(ss);   root->r = creat(ss);   return root;}void ccout(tree*root)//層次遍歷求出所有的葉節點{   queue<tree*>q;   tree*p =NULL;   if(root)   {      q.push(root);   }   while(!q.empty())   {      p = q.front();      q.pop();      if(p->l==NULL&&p->r==NULL)      {         cout<<p->data;      }      if(p->l)      q.push(p->l);      if(p->r)      q.push(p->r);   }}int main(){   char ss[102],*p;   while(~scanf("%s",ss))   {       p =  ss;       tree* root ;       root = creat(p);       ccout(root);       cout<<endl;   }}/***************************************************User name: jk160505徐紅博Result: AcceptedTake time: 0msTake Memory: 176KBSubmit time: 2017-02-07 16:26:35****************************************************/

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海城市| 航空| 龙井市| 平塘县| 舟曲县| 洛阳市| 原平市| 察雅县| 平昌县| 霍州市| 荣成市| 桂林市| 同仁县| 灌云县| 新乡市| 葵青区| 维西| 临夏市| 桃园县| 阿巴嘎旗| 庆城县| 彭水| 开江县| 阳谷县| 汪清县| 濮阳市| 佛冈县| 金昌市| 辽中县| 仪陇县| 景泰县| 银川市| 湾仔区| 海口市| 夹江县| 钟祥市| 常德市| 岐山县| 锡林郭勒盟| 马鞍山市| 大关县|