PAT1020 一个case出现段错误,可是我找不到

//PAT1020 二叉树 建树 层序遍历
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <queue>
using namespace std;
typedef int ElementType ;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
	ElementType Data;
	BinTree Left;
	BinTree right;
};

Position BuildTree(ElementType InOrder[],ElementType PostOrder[],int n){
	if(n==0)
	return NULL;
	 
	struct TNode* root=(struct TNode*)malloc(sizeof(struct TNode));
	root->Data=PostOrder[n-1];
	
	int n1;
	for(int i=0;i<n;i++)
	if(InOrder[i]==PostOrder[n-1])
	n1=i;
	
	root->Left=BuildTree(InOrder,PostOrder,n1);
	root->right=BuildTree(InOrder+n1+1,PostOrder+n1,n-n1-1);
	
	return root;
}

void LeverOrderTraversals(BinTree BT){//copy from mooc! 抄错了一次 line49  T->Left 不是BT->left 
	int flag=0;
	queue<struct TNode>q;
	BinTree T;
	if(!BT) return;
	q.push(*BT);
	while(!q.empty()){
		T=&q.front();
		q.pop();
		if(!flag){
			flag=1;
			printf("%d",T->Data);
		}
		else
		printf(" %d",T->Data);
		if(T->Left) q.push(*T->Left);
		if(T->right) q.push(*T->right);
	}	
};


int main(){
		int n;
		ElementType PostOrder[300],InOrder[300];
		scanf("%d",&n);
		for(int i=0;i<n;i++)
	    scanf("%d",&PostOrder[i]);
	    for(int i=0;i<n;i++)
	    scanf("%d",&InOrder[i]);
	    
	    Position root=BuildTree(InOrder,PostOrder,n);
	    LeverOrderTraversals(root);
	    
	    return 0;
}

  

posted on 2016-09-04 16:43  SijingLin  阅读(394)  评论(0编辑  收藏  举报

导航