sdut 3346 sdut 3344 Runtime Error Runtime Error?
生活随笔
收集整理的這篇文章主要介紹了
sdut 3346 sdut 3344 Runtime Error Runtime Error?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據結構實驗之二叉樹七:葉子問題
Time Limit:?1000MS?Memory Limit:?65536KB Submit?Statistic?DiscussProblem Description
已知一個按先序輸入的字符序列,如abd,,eg,,,cf,,,(其中,表示空結點)。請建立該二叉樹并按從上到下從左到右的順序輸出該二叉樹的所有葉子結點。
Input
輸入數據有多行,每一行是一個長度小于50個字符的字符串。Output
按從上到下從左到右的順序輸出二叉樹的葉子結點。Example Input
abd,,eg,,,cf,,, xnl,,i,,u,,Example Output
dfg uli #include <iostream>
#include <string.h>
#include <queue>
#include <stack>
#include <malloc.h>
using namespace std;
typedef struct btree
{char data;btree *lchild,*rchild;
}btree;
int i;
char ch[100];
btree *root;
btree * createbtree(btree *&root)
{char c;c=ch[i++];if(c==',')root=NULL;else{root=(btree *)malloc(sizeof(btree));root->data=c;root->lchild=createbtree(root->lchild);root->rchild=createbtree(root->rchild);}return root;
}
void levelorder()
{btree *b=root;queue<btree*>Queue;Queue.push(b);while(!Queue.empty()){b=Queue.front();Queue.pop();//這個沒有返回值if(b->lchild==NULL&&b->rchild==NULL)cout<<b->data;if(b->lchild!=NULL)Queue.push(b->lchild);if(b->rchild!=NULL)Queue.push(b->rchild);}cout<<endl;
}
int main()
{while(cin>>ch){i=0;root=createbtree(root);levelorder();}return 0;
}
數據結構實驗之二叉樹五:層序遍歷
Time Limit:?1000MS?Memory Limit:?65536KB Submit?Statistic?DiscussProblem Description
已知一個按先序輸入的字符序列,如abd,,eg,,,cf,,,(其中,表示空結點)。請建立二叉樹并求二叉樹的層次遍歷序列。
Input
輸入數據有多行,第一行是一個整數t?(t<1000),代表有t行測試數據。每行是一個長度小于50個字符的字符串。Output
輸出二叉樹的層次遍歷序列。Example Input
2 abd,,eg,,,cf,,, xnl,,i,,u,,Example Output
abcdefg xnuli #include <iostream>
#include <queue>
#include <malloc.h>
using namespace std;
typedef struct btree
{char data;btree *lchild,*rchild;
}btree;
char ch[55];
int i;
void createbtree(btree *&root)
{char c=ch[i++];if(c==',')root=NULL;else//按先序建立二叉樹,要是按照中序后序的順序自己應該會會寫出{root=(btree *)malloc(sizeof(btree));root->data=c;createbtree(root->lchild);createbtree(root->rchild);}
}
void levelorder(btree *&root)
{queue<btree*>Queue;btree *p=root;Queue.push(root);while(!Queue.empty()){p=Queue.front();cout<<p->data;Queue.pop();if(p->lchild!=NULL)Queue.push(p->lchild);if(p->rchild!=NULL)Queue.push(p->rchild);}
}
int main()
{int n;while(cin>>n){while(n--){i=0;cin>>ch;btree *root;root=(btree*)malloc(sizeof(btree));createbtree(root);levelorder(root);cout<<endl;}}return 0;
}/***************************************************
User name: YT1658506207邵雪源
Result: Runtime Error
Take time: 0ms
Take Memory: 0KB
Submit time: 2017-11-06 18:31:30
****************************************************/
總結
以上是生活随笔為你收集整理的sdut 3346 sdut 3344 Runtime Error Runtime Error?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sdut 2136 数据结构实验之二叉树
- 下一篇: sdut 3345 哈夫曼编码 优先队列