【问题描述】已知二叉树的后序遍历序列和中序遍历序列(二叉树中元素类型为字符类型),输出该二叉树的层次遍历序列。
【输入形式】二叉树的后序序列 二叉树的中序序列
【输出形式】二叉树的先序遍历序列
【样例输入】DIGEBHFCA DBGIEAFHC
【样例输出】
ABCDEFGHI
【样例说明】
【评分标准】
#include
using namespace std;
#include
#include
struct TNode {
char data;
TNode *lchild,*rchild;
};
typedef TNode *qelemtype;
struct QNode {
qelemtype data;
QNode *next;
};
struct LinkQueue {
QNode *front;
QNode *rear;
};
void initQueue(LinkQueue &Q) {
Q.front=Q.rear=new QNode;
Q.rear->next=NULL;
}
bool queueEmpty(LinkQueue Q) {
return Q.front->next==NULL;
}
void enQueue(LinkQueue &Q,qelemtype e) {
QNode *p;
p=new QNode;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
}
void deQueue(LinkQueue &Q,qelemtype &e) {
if(queueEmpty(Q)) {
cout<<"empty!"<next;
Q.front->next=p->next;
e=p->data;
if(Q.rear==p) {
Q.rear=Q.front;
}
delete p;
}
qelemtype deQueue(LinkQueue &Q) {
qelemtype e;
if(queueEmpty(Q)) {
cout<<"empty!"<next;
Q.front->next=p->next;
e=p->data;
if(Q.rear==p) {
Q.rear=Q.front;
}
delete p;
return e;
}
void getFront(LinkQueue Q,qelemtype &e) {
if(queueEmpty(Q)) {
cout<<"empty!"<next;
e=p->data;
}
qelemtype getFront(LinkQueue Q) {
qelemtype e;
if(queueEmpty(Q)) {
cout<<"empty!"<next;
e=p->data;
return e;
}
int location(char *in,int n,char ch) {
for(int index=0; indexdata=*(post+n-1);
int index=location(in,n,*(post+n-1));
createTree(T->lchild,post,in,index);
createTree(T->rchild,post+index,in+index+1,n-index-1);
}
}
void levelOrder(TNode *T) {
LinkQueue Q;
initQueue(Q);
TNode *p;
p=T;
if(p) {
enQueue(Q,p);
}
while(!queueEmpty(Q)) {
deQueue(Q,p);
cout<data;
if(p->lchild)
enQueue(Q,p->lchild);
if(p->rchild)
enQueue(Q,p->rchild);
}
}
int main() {
TNode *T;
char post[30],in[30];
cin>>post>>in;
createTree(T,post,in,strlen(in));
levelOrder(T);
return 0;
}
#include
using namespace std;
string post, in;
int len, height, leaf;
typedef struct Node {
char data;
struct Node* lchild, * rchild;
}Node, * BTree;
BTree buildBTree(int pl, int pr, int il, int ir) {
if (pl > pr) return NULL;
Node* root = new Node();
root->data = post[pr];
int pos = in.find(post[pr]);
root->lchild = buildBTree(pl, pl + pos - il - 1, il, pos - 1);
root->rchild = buildBTree(pl + pos - il, pr - 1, pos + 1, ir);
return root;
}
void levelOrder(BTree root) {
if (root == NULL) return;
queue q;
q.push(root);
while (!q.empty()) {
BTree tmp = q.front();
q.pop();
cout << tmp->data;
if (tmp->lchild)
q.push(tmp->lchild);
if (tmp->rchild)
q.push(tmp->rchild);
}
}
void preOrder(BTree root) {
if (root == NULL) return;
cout << root->data;
preOrder(root->lchild);
preOrder(root->rchild);
}
int main()
{
cin >> post >> in;
len = post.length();
Node* root = buildBTree(0, len - 1, 0, len - 1);
levelOrder(root);
return 0;
}
#include
#include
#include
using namespace std;
struct Node {
char data;
Node* lchild;
Node* rchild;
};
Node* buildBTree(string post, string in, int pl, int pr, int il, int ir) {
if (pl > pr)
return NULL;
Node* root = new Node();
root->data = post[pr];
int pos = in.find(post[pr]);
root->lchild = buildBTree(post, in, pl, pl + pos - il - 1, il, pos - 1);
root->rchild = buildBTree(post, in, pl + pos - il, pr - 1, pos + 1, ir);
return root;
}
void levelOrder(Node* root) {
if (root == NULL)
return;
queue q;
q.push(root);
while (!q.empty()) {
Node* tmp = q.front();
q.pop();
cout << tmp->data;
if (tmp->lchild)
q.push(tmp->lchild);
if (tmp->rchild)
q.push(tmp->rchild);
}
}
int main() {
string post, in;
cin >> post >> in;
Node* root = buildBTree(post, in, 0, post.length() - 1, 0, in.length() - 1);
levelOrder(root);
return 0;
}