List Leaves

先粘下题目

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5
这道题目要注意下输出格式

先解释下题目的意思,

输入格式:

第一行是结点的总数n

后面每行表示的是每个结点 的左孩子和右孩子的编号,比如下面的第一行就是表示 0 这个结点的左孩子是1,右孩子为空

输出格式:

就是按照从上至下,从左至右的顺序(也就是层次遍历)输出叶子结点,注意最后一个后面不能有空格


以下就是代码,自己测试是可以通过,但是测试时PAT显示有两个测试点说是段错误,后面再进行改进,也希望大家帮忙改进

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

struct TreeNode
{
	TreeNode *left;
	TreeNode *right;
	int data;
	TreeNode()
	{
	}
	TreeNode(int d, TreeNode *l, TreeNode *r)
	{
		data = d;
		left = l;
		right = r;
	}
};

typedef struct myNode
{
	char left;
	char right;
	myNode(char l, char r)   //构造函数
	{
		left = l;
		right = r;
	}
}myNode;

int main()
{
	vector<myNode> vec;
	vector<TreeNode> tree;
	int n;   //输入第一行的整数
	cin >> n;
	char a, b;
	//输入后面的n行数据并存储到 vec 中
	for (int i = 0; i < n; i++)
	{
		cin >> a >> b;
		vec.push_back(myNode(a, b));
	}

	//生成树的n个结点 放入 tree 中
	for (int i = 0; i < n; i++)
	{
		tree.push_back(TreeNode(i, NULL, NULL));
	}

	//建立一颗二叉树
	for (int i = 0; i < n; i++)
	{
		if (vec[i].left != '-')
		{
		 tree[i].left = &tree[vec[i].left - '0'];
		}
		else
		{
			tree[i].left = NULL;
		}
		if (vec[i].right != '-')
		{
			tree[i].right = &tree[vec[i].right - '0'];
		}
		else
		{
			tree[i].right = NULL;
		}
	}

	//寻找根结点
	vector<bool> flag;
	for (int i = 0; i < n; i++)
		flag.push_back(false);
	TreeNode root;
	for (int i = 0; i < n; i++)
	{
		if (vec[i].left != '-')
			flag[vec[i].left - '0'] = true;
		if (vec[i].right != '-')
			flag[vec[i].right - '0'] = true;
	}
	for (int i = 0; i < n; i++)
	{
		if (flag[i] == false)
		{
			root.data = i;
			root.left = &tree[vec[i].left - '0'];
			root.right = &tree[vec[i].right - '0'];
		}
	}

	//层次遍历,使用队列
	queue<TreeNode> que;
	que.push(root);
	TreeNode temp;
	
	//将所有的结点入队
	for (int i = 1; i <= n; i++)
	{
		temp = que.front();
		if (temp.left != NULL)
		{
			que.push(tree[temp.left->data]);						
		}
		if (temp.right != NULL)
			que.push(tree[temp.right->data]);
		if (temp.left == NULL && temp.right == NULL)
		{
			cout << temp.data;
			if (i != n )
				cout << " ";
		}
		que.pop();
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值