灵异事件求解
  • 板块学术版
  • 楼主sunyizhe还是MC大佬
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/11/13 23:09
  • 上次更新2023/11/14 12:09:00
查看原帖
灵异事件求解
481330
sunyizhe还是MC大佬楼主2023/11/13 23:09

为了方便理解,代码含义是这样的:给一个二叉树的前序遍历(空子树用 "," 表示). 求出它的叶子节点.(环境: NOI Linux 2.0)

#include <bits/stdc++.h>
using namespace std;
const int N=60;

struct Node{
	char dat;
	int l,r;
}t[N];

int tot;
int build()
{
	char c;
	if(scanf("%c",&c)!=1)exit(0);

	if(c==',')return 0;
	
	int rt=++tot;
	t[rt].dat=c;
	t[rt].l=build();
	t[rt].r=build();
	
	return rt;
}

void print()
{
	queue<int> q;
	q.push(1);
	
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		
		if(t[u].l==0&&t[u].r==0)printf("%c",t[u].dat);
		if(t[u].l!=0)q.push(t[u].l);
		if(t[u].r!=0)q.push(t[u].r);
	}
}

void fast_read()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

int main()
{
	//fast_read();
	
	while(true)
	{
		memset(t,0,sizeof(t));
		tot=0;
		cout<<tot<<endl;
		build();
		cout<<tot<<endl;
		print();
		
		printf("\n");
	}
	
	return 0;
}

结果每次都是第一个数据能给出正确答案,但后面的数据程序像卡住了一样,但既没有 RE,输入数据还可以正常输入.

2023/11/13 23:09
加载中...