pat1020

<h1 style="margin: 0px 0px 0.5em; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; font-weight: normal; font-stretch: inherit; line-height: 1em; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; vertical-align: baseline; color: rgb(17, 17, 17); background-color: rgb(250, 250, 250);"><span style="font-size:12px;">1020. Tree Traversals (25)</span></h1><div id="problemInfo" class="rfloat" style="margin: 0px; padding: 2px; border: 1px solid rgb(221, 221, 221); border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; font-stretch: inherit; line-height: 18px; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; vertical-align: baseline; float: right; text-align: center; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);"><div class="limit" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);"><span style="font-size:12px;">时间限制</span></div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><span style="font-size:12px;">400 ms
</span></div></div><div class="limit" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);"><span style="font-size:12px;">内存限制</span></div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><span style="font-size:12px;">65536 kB
</span></div></div><div class="limit" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);"><span style="font-size:12px;">代码长度限制</span></div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><span style="font-size:12px;">16000 B
</span></div></div><div class="checker" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);"><span style="font-size:12px;">判题程序</span></div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><span title="纯文本对比" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><span style="font-size:12px;">Standard</span></span></div></div><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);"><span style="font-size:12px;">作者</span></div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline;"><span style="font-size:12px;">CHEN, Yue
</span></div></div><div id="problemContent" style="margin: 0px; padding: 0px; border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; font-stretch: inherit; line-height: 18px; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; vertical-align: baseline; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; border: 0px; font-stretch: inherit; vertical-align: baseline;"><span style="font-size:12px;">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.</span></p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; border: 0px; font-stretch: inherit; vertical-align: baseline;"><strong><span style="font-size:12px;">Input Specification:</span></strong></p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; border: 0px; font-stretch: inherit; vertical-align: baseline;"><span style="font-size:12px;">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.</span></p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; border: 0px; font-stretch: inherit; vertical-align: baseline;"><strong><span style="font-size:12px;">Output Specification:</span></strong></p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; border: 0px; font-stretch: inherit; vertical-align: baseline;"><span style="font-size:12px;">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.</span></p><strong><span style="font-size:12px;">Sample Input:</span></strong><pre style="margin-top: 1.5em; margin-bottom: 1.5em; line-height: 1.5; padding: 0px; border: 0px; font-stretch: normal; font-family: 'Droid Sans Mono', Consolas, 'Courier New', monospace; vertical-align: baseline; overflow: auto;"><span style="font-size:12px;">7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
</span>
Sample Output:

4 1 6 3 5 7 2

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


 
<span style="font-size:14px;">#include <iostream>
#include<vector>
#include<queue>
#include<fstream>
using namespace std;

struct TreeNode{
    int val;
    struct TreeNode *l,*r;
    TreeNode(){l=NULL,r=NULL,val=-1;}
};
int pre[35],ps;
int bfs[35],bf;
//TreeNode *root;

TreeNode* createTree(int post[],int pl,int pr,int in[],int il,int ir )
{
    if(pl > pr)
    {
        return NULL;
    }
    TreeNode* root = new TreeNode;
    root->val = post[pr];
    int k;
    for(k = 0;k <= ir;k++)
    {
        if(post[pr] == in[k])
            break;
    }

    int numleft = k - il;
    root->l = createTree(post,pl,pl+numleft-1,in,il,il+k-1);
    root->r = createTree(post,pl+numleft,pr-1,in,k+1,ir);
    return root;
}

void preOrder(TreeNode* node)
{
    if(node == NULL)
        return ;
    //cout<<node->val;
    pre[ps++]=node->val;
    preOrder(node->l);
    preOrder(node->r);

}

void BFS(TreeNode* root)
{
    queue<TreeNode*> qq;
    qq.push(root);

    while(!qq.empty())
    {
        if(qq.front()->l!=NULL)
            qq.push(qq.front()->l);
        if(qq.front()->r!=NULL)
            qq.push(qq.front()->r);
        bfs[bf++]=qq.front()->val;

        qq.pop();
    }
}
int main()
{
    ifstream cin("input.txt");
    int p[35],in[35];
    int n,t;
    cin>>n;
    for(int i = 0;i < n;i ++)
    {
        cin>>p[i];
       // cout<<p[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>in[i];
    }

    TreeNode* root = createTree(p,0,n-1,in,0,n-1);
    TreeNode*root2 = root;
    //preOrder(root);
    //for(int i=0;i<n-1;i++)
      //  printf("%d ",pre[i]);
    //printf("%d",pre[n-1]);
    //cout<<"1111111111";
    BFS(root2);


    for(int i=0;i<n-1;i++)
        printf("%d ",bfs[i]);
    printf("%d",bfs[n-1]);
    return 0;
}
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值