文章

60

粉丝

361

获赞

41

访问

497.4k

头像
一个弱鸡思路
P1317 浙江大学机试题
发布于2021年1月20日 09:55
阅读数 8.8k

弱鸡想法:
前序和中序确定一个树

所以求出每个树的前序和中序序列然后看是否相同

#include <iostream>
#include <string>
#include <string.h>
using namespace std;
string preorder_1="";
string midorder_1="";
string preorder_2="";
string midorder_2="";
typedef struct node
{
	int data;
	struct node *lchild,*rchild;
}*tree;

void paixu(tree &T,int temp)
{
	if(T==NULL)
	{
		T=new node;
		T->data=temp;
		T->lchild =NULL;
		T->rchild=NULL;
		return;
	}
	else
	{
		if(temp==T->data)
			return;
		else if(temp<T->data)
			paixu(T->lchild ,temp);
		else if(temp>T->data)
			paixu(T->rchild ,temp);
	}
	return;
}
void preorder(tree T)
{
	if(T!=NULL)
	{
		preorder_1+=T->data+'0';
		preorder(T->lchild);
		preorder(T->rchild);
	}
}
void midorder(tree T)
{
	if(T!=NULL)
	{
		midorder(T->lchild);
		midorder_1+=T->data +'0';
		midorder(T->rchild);
	}
}

int main()
{
	int n;
	while(cin>>n)
	{
		tree T=NULL;...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发