ZOJ3170 7 Levels of Binary Search Tree,BST

11 篇文章 0 订阅
2 篇文章 0 订阅

这题可以先对那些数字进行快排。排序结束后,就可以根据给出的对于某个节点,左右节点的孩子数来确定这个节点的数据是那个数了。注意要留意n==1的情况,在这里WA了很久。


/*******************************************************************************
 # Author : Neo Fung
 # Email : neosfung@gmail.com
 # Last modified: 2011-09-12 10:10
 # Filename: ZOJ3170 7 Levels of Binary Search Tree.cpp
 # Description : 
 ******************************************************************************/
// ZOJ3170 7 Levels of Binary Search Tree.cpp : Defines the entry point for the console application.
//

// #include "stdafx.h"



#include <stdio.h>
#include <iostream>
#include <string>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

int num[256];
vector<int> result;

struct node 
{
	int data;
	struct node *left,*right;
}*Head;
void insert(int elem,struct node * & p)
{
	if( p == NULL )
	{
		p = (struct node*)malloc(sizeof(struct node));
		p->data = elem;
		p->left = p->right = NULL;
		return ;
	}
	if( elem > p->data )
		insert(elem,p->right);
	else 
		insert(elem,p->left);
}

void postorder(struct node *p)
{
	if(p==NULL) return;
	else
	{
		postorder(p->left);
		postorder(p->right);
		result.push_back(p->data);
	}
}


int main(void)
{
	int n,l;
	int a,b,c;
	queue<int> start,index,end;

	while(scanf("%d",&n)!=EOF)
	{
		memset(num,0,sizeof(num));
		result.clear();
		for(int i=0;i<n;++i)
			scanf("%d",&num[i]);
		sort(num,num+n);
		scanf("%d",&l);
		if (n==1)
		{
			printf("%d\n",num[0]);
			continue;
		}
		scanf("%d %d",&a,&c);
		start.push(0);
		index.push(a);
		end.push(a+c);

		while(!start.empty())
		{
			a=start.front();
			b=index.front();
			c=end.front();
			start.pop(),index.pop(),end.pop();

			insert(num[b],Head);
			if(b-a>0)
			{
				int temp1=0,temp2=0;
				if(b-a>1)
					scanf("%d %d",&temp1,&temp2);
				start.push(a);index.push(a+temp1);end.push(a+temp1+temp2);
			}
			if(c-b>0)
			{
				int temp1=0,temp2=0;
				if(c-b>1)
					scanf("%d %d",&temp1,&temp2);
				start.push(b+1);index.push(b+1+temp1);end.push(b+1+temp1+temp2);
			}
		}
		postorder(Head);
		printf("%d",result[0]);
		for(int i=1;i<n;++i)
			printf(" %d",result[i]);
		Head=NULL;
		printf("\n");
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值