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

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

sdutacm-數據結構實驗之二叉樹五:層序遍歷

2019-11-06 06:48:07
字體:
來源:轉載
供稿:網友

數據結構實驗之二叉樹五:層序遍歷

TimeLimit: 1000MS Memory Limit: 65536KB

SubmitStatistic

PRoblem Description

已知一個按先序輸入的字符序列,如abd,,eg,,,cf,,,(其中,表示空結點)。請建立二叉樹并求二叉樹的層次遍歷序列。

Input

 輸入數據有多行,第一行是一個整數t (t<1000),代表有t行測試數據。每行是一個長度小于50個字符的字符串。

Output

 輸出二叉樹的層次遍歷序列。

Example Input

2

abd,,eg,,,cf,,,

xnl,,i,,u,,

Example Output

abcdefg

xnuli

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){   if(*ss==',')   {   ss++;   return NULL;   }  tree*p;  p = (tree*)malloc(sizeof(tree));  p->data = *ss++;  p->l = creat(ss);  p->r = creat(ss);  return p;}void lastout(tree*p){   if(p)   {       lastout(p->l);       lastout(p->r);       printf("%c",p->data);   }}void inout (tree*p){   if(p)   {     inout(p->l);     printf("%c",p->data);     inout(p->r);   }}int num;void sumyz(tree*p){   if(p)   {     if(p->l==NULL&&p->r==NULL)     {        num++;     }     sumyz(p->l);     sumyz(p->r);   }}int deep(tree*p){     if(!p)     return 0;     else     {        return (max(deep(p->l),deep(p->r))+1);     }}void ccout(tree*root){   queue<tree *>q;   tree* p = NULL;   if(root)   {      q.push(root);   }   while(!q.empty())   {      p = q.front();      q.pop();      cout<<p->data;      if(p->l)      {         q.push(p->l);      }      if(p->r)      {         q.push(p->r);      }   }}int main(){   char ss[52],*p;   int o;   cin>>o;   while(o--)   {     scanf("%s",ss);     p = ss;     tree* root;     root = creat(p);     ccout(root);     cout<<endl;   }}/***************************************************User name: jk160505徐紅博Result: AcceptedTake time: 0msTake Memory: 168KBSubmit time: 2017-02-07 14:47:43****************************************************/


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玉龙| 微山县| 丹凤县| 武鸣县| 阿坝| 和平县| 车致| 阳西县| 南涧| 河西区| 樟树市| 乌海市| 阿荣旗| 苍山县| 舟曲县| 杂多县| 桃园市| 体育| 大田县| 宁化县| 霍城县| 威信县| 科尔| 昭苏县| 无极县| 斗六市| 敖汉旗| 玉山县| 交口县| 邢台县| 杂多县| 抚松县| 西乌珠穆沁旗| 迭部县| 上林县| 丹东市| 高安市| 卢龙县| 泌阳县| 滦平县| 鸡泽县|