PAT甲级1020 Tree Traversals (25 分)

1.给出中序遍历和后序遍历输出先序遍历

我们是怎么完成这个操作的呢?

主要的思想是:通过后序遍历找到根节点,通过中序遍历把左右子树分开,并记录左子树中节点的个数。通过记录的左子树节点的个数,准确找到左子树、右子树在中序、后序遍历中的位置。

 

in[]中序遍历的数组

post[]后序遍历的数组

在后序遍历中其最后一个节点为根节点,记为root

在中序遍历中找到一个节点值与root相同,即为下标i,这样就可以将树分为左右两部分了。

用numLeft标记左子树中节点的个数,左子树的节点个数为numLeft=i-inL;

建树的函数:

create(int postL,int postR,int inL,int inR);

postL是在后序遍历中左端点的位置,postR是后序遍历中右端点的位置;

inL是在中序遍历中左端点的位置,inR是中序遍历中右端点的位置。

对于左子树 传参 (postL,postL+numLeft-1,inL,i-1)

对于右子树 传参 (postL+numLeft,postR-1,i+1,inR)

 

 1 #include <unordered_map>
 2 #include <vector>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 int in[100],post[100],n;
 7 struct node
 8 {
 9     int data;
10     node* left;
11     node* right;
12 };
13 node* create(int postL,int postR,int inL,int inR){
14     if(postL>postR)return NULL;
15     node* root=new node;
16     root->data=post[postR];
17     int i;
18     for(i=inL;i<=inR;i++){//在中序遍历的正确范围中找与根节点相同的值
19         if(in[i]==post[postR])break;
20     }
21     int numLeft;
22     numLeft=i-inL;
23     root->left=create(postL,postL+numLeft-1,inL,i-1);
24     root->right=create(postL+numLeft,postR-1,i+1,inR);
25     return root;
26 }
27 void pre(node* root)
28 {
29     cout<<root->data<<" ";
30     if(root->left!=NULL)pre(root->left);
31     if(root->right!=NULL)pre(root->right);
32 }
33 int main()
34 {
35     cin>>n;
36     int i;
37     for (int i = 0; i < n; ++i) {
38         cin>>in[i];
39     }
40     for (int i = 0; i < n; ++i) {
41        cin>>post[i];
42     }
43     node* root=create(0,n-1,0,n-1);
44     pre(root);
45     return 0;
46 }

层次遍历

void order(node* t)
{
    if(!t)return;
    node* temp;
    queue<node*>q;
    q.push(t);
    while (!q.empty())
    {
        temp=q.front();
        a.push_back(temp->data);
        if(temp->left)q.push(temp->left);
        if(temp->right)q.push(temp->right);
        q.pop();
    }
}

 

 

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.

Input Specification:

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.

Output Specification:

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.

Sample Input:

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

Sample Output:

4 1 6 3 5 7 2

 

题意很简单,就是给出后序中序按层次遍历输出。

 1 #include <unordered_map>
 2 #include <vector>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include<queue>
 6 using namespace std;
 7 int in[100],post[100],n;
 8 struct node
 9 {
10     int data;
11     node* left;
12     node* right;
13 };
14 node* create(int postL,int postR,int inL,int inR)
15 {
16     if(postL>postR)return NULL;
17     node* root=new node;
18     root->data=post[postR];
19     int i;
20     for(i=inL;i<=inR;i++){
21         //if(in[i]==root->data)
22         if(in[i]==post[postR])
23             break;
24     }
25     int numLeft=i-inL;
26     root->left=create(postL,postL+numLeft-1,inL,i-1);
27     root->right=create(postL+numLeft,postR-1,i+1,inR);
28     return root;
29 }
30 vector<int>a;
31 void order(node* t)
32 {
33     if(!t)return;
34     node* temp;
35     queue<node*>q;
36     q.push(t);
37     while (!q.empty())
38     {
39         temp=q.front();
40         a.push_back(temp->data);
41         if(temp->left)q.push(temp->left);
42         if(temp->right)q.push(temp->right);
43         q.pop();
44     }
45 }
46 int main()
47 {
48     cin>>n;
49     for(int i=0;i<n;i++)
50         cin>>post[i];
51     for(int i=0;i<n;i++)
52         cin>>in[i];
53     node* root=create(0,n-1,0,n-1);
54     order(root);
55     for(int i=0;i<a.size();i++)
56     {
57         if(i==0)cout<<a[i];
58         else cout<<" "<<a[i];
59     }
60     return 0;
61 }

 

posted @ 2019-08-23 19:09  zuiaimiusi  阅读(272)  评论(0编辑  收藏  举报