PAT甲级.1020. Tree Traversals (25)

来源:互联网 发布:埃勒里奎因知乎 编辑:程序博客网 时间:2024/05/21 16:42

题目

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

输入格式

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

输出格式

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

输出样例

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

输出样例

4 1 6 3 5 7 2


PAT链接

思路

关键就是如何用后序遍历序列和中序遍历序列来重建二叉树。
1.后序从后向前遍历,最后一个元素就是根结点
2.后序中每个元素把中序序列分成左子树和右子树两部分。

另外还需要注意层序遍历的实现

代码

/*** @tag     PAT_A_1020* @authors R11happy (xushuai100@126.com)* @date    2016-10-21 11:48-16:23* @version 1.0* @Language C++* @Ranking  645/1723* @function null*/#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>    //用queue要单独声明#include <algorithm>using namespace std;int in[40];int post[40];struct node{    int data;    node *lchild;    node *rchild;};node *create(int postL, int postR, int inL, int inR){    if (postL > postR)   return NULL;    node *root = new node;    root->data = post[postR];    int k;    /*中序序列中找到要搜索的元素*/    for (k = inL; k <= inR; k++)    {        if (in[k] == post[postR])  break;    }    int numLeft = k - inL;    root->lchild = create(postL, postL + numLeft - 1, inL, k - 1);    root->rchild = create(postL + numLeft, postR - 1, k + 1, inR);    return root;}/*要输出的是层序遍历,非前序遍历*/void prePrint(node *T){    if (T == NULL)   return;    printf("%d ", T->data);    prePrint(T->lchild);    prePrint(T->rchild);}int N;  //结点个数int num;    //已输出结点个数void BFS(node *root){    queue<node*> q; //注意队列里存的都是地址    q.push(root);    while (!q.empty())    {        node *now = q.front();        q.pop();        printf("%d", now->data);        num++;        if (num < N) printf(" ");        if (now->lchild != NULL) q.push(now->lchild);        if (now->rchild != NULL) q.push(now->rchild);    }}int main(int argc, char const *argv[]){    scanf("%d", &N);    for (int i = 0; i<N; i++)    {        scanf("%d", &post[i]);    }    for (int i = 0; i<N; i++)    {        scanf("%d", &in[i]);    }    node *T = create(0, N - 1, 0, N - 1);    BFS(T);    return 0;}/*Sample Input:72 3 1 5 7 6 41 2 3 4 5 6 7Sample Output:4 1 6 3 5 7 2 */

收获

1.后序遍历+中序遍历重建二叉树

node *create(int postL, int postR, int inL, int inR){    if (postL > postR)   return NULL;//注意递归出口    node *root = new node;    root->data = post[postR];    int k;    /*中序序列中找到要搜索的元素*/    for (k = inL; k <= inR; k++)    {        if (in[k] == post[postR])  break;    }    int numLeft = k - inL;    root->lchild = create(postL, postL + numLeft - 1, inL, k - 1);    root->rchild = create(postL + numLeft, postR - 1, k + 1, inR);    return root;}

2.层序遍历

void BFS(node *root){    queue<node*> q; //注意队列里存的都是地址    q.push(root);    while (!q.empty())    {        node *now = q.front();        q.pop();        printf("%d", now->data);        num++;        if (num < N) printf(" ");        if (now->lchild != NULL) q.push(now->lchild);        if (now->rchild != NULL) q.push(now->rchild);    }}
0 0
原创粉丝点击