欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

List Leaves

程序员文章站 2022-05-17 21:24:30
...

03-树2 List Leaves(25 分)

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 N1. 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

#include<iostream>
#include<vector>
#include<fstream>
#include<queue>
#include<algorithm>
using namespace std;
//ifstream inFile("C:\\Users\\DELL\\Desktop\\in.txt",ios::in);
typedef struct Node* Tree;
struct Node{
	Tree left, right;
	int date;
};
struct node{
	int left, right;
};
vector<node> Nodes;//***************************************************************
Tree CreateTree(int index)
{
	Tree tree = new Node;
	if(Nodes[index].left ==-1&&Nodes[index].right ==-1){
		tree->date = index;
		tree->left = tree->right = nullptr;
	}else if(Nodes[index].left ==-1&&Nodes[index].right !=-1){
		tree->date = index;
		tree->left = nullptr;
		tree->right = CreateTree(Nodes[index].right );
	}else if(Nodes[index].left != -1 && Nodes[index].right == -1){
		tree->date = index;
		tree->right = nullptr;
		tree->left = CreateTree(Nodes[index].left );
	}else if (Nodes[index].left != -1 && Nodes[index].right != -1) {
		tree->date = index;
		tree->left = CreateTree(Nodes[index].left);
		tree->right = CreateTree(Nodes[index].right);
	}
	return tree;
}
void PrePrint(Tree tree)
{
	if(tree!=nullptr){
		cout << tree->date << endl;
		PrePrint(tree->left);
		PrePrint(tree->right);
	}
}
int InsertNodes()
{
	int N;
	cin >> N;
	int* a = new int[N];
	fill(a, a + N, 0);
	for(int i=0;i<N;++i){
		node d;
		char m1, m2;
		cin >> m1 >> m2;
		if(m1=='-'){
			d.left = -1;
		}else{
			d.left = m1 - '0';
			a[m1 - '0'] = 1;
		}
		if(m2=='-'){
			d.right = -1;
		}else{
			d.right = m2 - '0';
			a[m2 - '0'] = 1;
		}
		Nodes.push_back(d);
	}
	int ret = -1;
	for(int i=0;i<N;++i){
		if(a[i]==0){
			ret = i;
			break;
		}
	}
	return ret;
}
vector<int> Leaves(Tree tree)
{
	vector<int> leaves;
	queue<Tree> Node;
	Node.push(tree);
	while(Node.size()){
		Tree tem = Node.front();
		Node.pop();
		if (tem->left == nullptr&&tem->right == nullptr) {
			leaves.push_back(tem->date);
		}
		if(tem->left )
		Node.push(tem->left);
		if(tem->right )
		Node.push(tem->right);
	}
	return leaves;
}
template<typename T>
void Print(T first,T last)
{
	int count = 0;
	for(;first!=last;++first){
		count++;
		if(count==1){
			cout << *first;
		}else{
			cout << " " <<*first;
		}
	}
	cout << endl;
}
int main()
{
	int index = InsertNodes();
	Tree tree=CreateTree(index);
	//PrePrint(tree);
	vector<int> m;
	m = Leaves(tree);
	Print(m.begin(), m.end());
	system("pause");
	return 0;
}