本文主要是介绍sdut 3346 sdut 3344 Runtime Error Runtime Error?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
数据结构实验之二叉树七:叶子问题
Problem Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。
Input
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;
}
#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;
}
数据结构实验之二叉树五:层序遍历
Problem Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立二叉树并求二叉树的层次遍历序列。
Input
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
****************************************************/
#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?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!