sicily-1156. Binary tree

1156. Binary tree


限制条件
时间限制: 1 秒, 内存限制: 32 兆


题目描述


Your task is very simple: Given a binary tree, every node of which contains one upper case character (‘A’ to ‘Z’); you just need to print all characters of this tree in pre-order.


输入格式

Input may contain several test data sets.
      For each test data set, first comes one integer n (1 <= n <= 1000) in one line representing the number of nodes in the tree. Then n lines follow, each of them contains information of one tree node. One line consist of four members in order: i (integer, represents the identifier of this node, 1 <= i <= 1000, unique in this test data set), c (char, represents the content of this node described as above, ‘A’ <= c <= ‘Z’), l (integer, represents the identifier of the left child of this node, 0 <=  l <= 1000, note that when l is 0 it means that there is no left child of this node), r (integer, represents the identifier of the right child of this node, 0 <=  r <= 1000, note that when r is 0 it means that there is no right child of this node). These four members are separated by one space.
      Input is ended by EOF.
      You can assume that all inputs are valid. All nodes can form only one valid binary tree in every test data set.

输出格式


For every test data set, please traverse the given tree and print the content of each node in pre-order. Characters should be printed in one line without any separating space.


样例输入
3
4 C 1 3
1 A 0 0
3 B 0 0
1
1000 Z 0 0
3
1 Q 0 2
2 W 3 0
3 Q 0 0


样例输出
CAB
Z
QWQ


题目来源
ZSUACM Team Member

这道题大概意思就是要你输出一颗二叉树的前序就行了,题目有多个测试样例,第一行输入为n(1<=n<=1000),剩下有n行,每行分别为i(节点的序号,1<=i<=1000,整形),c(节点的字符,'A'<=c<='Z',char类型),l,r(分别为左子数和右子数的序号,1<=l,r<=1000)。

这道题的关键点是找到树根:没有出现在左子树和右子树处的数字即为数根的序号

//1156. Binary tree
//2016.11.23

#include <iostream>
#include <cstring>

using namespace std;

struct Node
{
	char data;
	int lchild;
	int rchild;
	bool flag;   //标记是否是树根 
};

Node n[1005];
int a[1005];

void p(int x)   //深搜 
{
	
	if(x==0)
		return;
	else
	{	
		cout<<n[x].data;
		p(n[x].lchild);
		p(n[x].rchild);
	}
}

int main()
{
	int m,k;
	while(cin>>m)
	{
		for(int i=1;i<=m;i++)
		{
			cin>>k;
			a[i]=k;
			cin>>n[k].data>>n[k].lchild>>n[k].rchild;
			n[k].flag = true;
		}
		for(int i=1;i<=m;i++)   // 将左右子树 出现的数对应节点标记 
		{
			n[n[a[i]].lchild].flag = false;
			n[n[a[i]].rchild].flag = false;
		}
		int cnt;
		for(int i=1;i<=m;i++)  //找出根 
		{
			if(n[a[i]].flag ==true)
				cnt=a[i];
		}
		p(cnt);
		cout<<endl;
		
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值