| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1810 人关注过本帖
标题:段错误
只看楼主 加入收藏
流照君
Rank: 2
等 级:论坛游民
帖 子:66
专家分:74
注 册:2018-5-7
结帖率:70%
收藏
已结贴  问题点数:20 回复次数:3 
段错误
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
就是给出二叉树的后序和中序
求出二叉树的层次遍历
程序代码:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=105;
int in[inf],post[inf],n;
struct node{
    int data;
    struct node * lchild;
    struct node * rchild;
};
void bfs(node * root)
{
    queue<node*> q;
    q.push(root);
    while(!q.empty())
    {
        node *p=q.front();
        cout<<p->data<<" ";
        q.pop();
        if(p->lchild !=NULL)
        q.push(p->lchild);
        if(p->rchild !=NULL)
        q.push(p->rchild);    
    }
}
node * creat_tree(int postl,int postr,int inl,int inr)
{
    if(postl<postr)
    return NULL;
    node * root=new node;
    root->data=post[postr];
    int i=postl;
    while(i<inr)
    {
        if(post[postr]==in[i])
        break;
        i++;
    }
    int num=i-inl;//左子树节点个数 
    root->lchild=creat_tree(postl,postr+num-1,inl,i-1);
    root->rchild=creat_tree(postr+num,postr-1,i+1,inr);
    return root;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>post[i];
    for(int i=0;i<n;i++)
    cin>>in[i];
    node * root=creat_tree(0,n-1,0,n-1);
    bfs(root);
    return 0;

 } 

不知道为什么出现段错误???
求大神来解答
搜索更多相关主题的帖子: the int sequence line node 
2019-07-13 15:05
流照君
Rank: 2
等 级:论坛游民
帖 子:66
专家分:74
注 册:2018-5-7
收藏
得分:0 
在线等,挺急的
2019-07-13 15:08
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
如果你调试一下,就会发现
node * creat_tree(int postl,int postr,int inl,int inr)
{
    if(postl<postr)
    return NULL;
直接返回了 NULL
2019-07-15 09:18
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
终于找到题目链接了:https://

程序代码:
#include <queue>
#include <vector>
#include <algorithm>

std::vector<unsigned> foo( size_t n, const unsigned postorder[], const unsigned inorder[] )
{
    std::vector<unsigned> result;
    result.reserve( n );

    struct bar
    {
        size_t n;
        const unsigned* postorder;
        const unsigned* inorder;

        bar( size_t n, const unsigned* postorder, const unsigned* inorder ) : n(n), postorder(postorder), inorder(inorder)
        {
        }
    };

    std::queue<bar> buf;
    for( buf.push(bar(n,postorder,inorder)); !buf.empty(); buf.pop() )
    {
        const bar& r = buf.front();
        result.push_back( r.postorder[r.n-1] );

        const unsigned* p = std::find( r.inorder, r.inorder+r.n, r.postorder[r.n-1] );
        if( p-r.inorder != 0 )
            buf.push( bar(p-r.inorder,r.postorder,r.inorder) );
        if( r.n+r.inorder-p-1 != 0 )
            buf.push( bar(r.n+r.inorder-p-1, p-r.inorder+r.postorder ,p+1) );
    }

    return result;
}

#include <cstdio>

int main( void )
{
    size_t n;
    unsigned postorder[30], inorder[30];
    scanf( "%zu", &n );
    for( size_t i=0; i!=n; ++i )
        scanf( "%u", &postorder[i] );
    for( size_t i=0; i!=n; ++i )
        scanf( "%u", &inorder[i] );

    std::vector<unsigned> result = foo( n, postorder, inorder );

    for( size_t i=0; i!=result.size(); ++i )
        printf( "%u%c", result[i], " \n"[i+1==result.size()] );
}



2019-07-15 10:25
快速回复:段错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017435 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved