sicily 1156 二叉树的遍历 前序遍历,递归,集合操做

1156. Binary tree

Constraints

Time Limit: 1 secs, Memory Limit: 32 MBnode

Description

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

Input

 

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

      Input is ended by EOF.this

      You can assume that all inputs are valid. All nodes can form only one valid binary tree in every test data set.spa

 

Output

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

Sample Input

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

Sample Output

CAB
Z
QWQ

Problem Source

ZSUACM Team Memberblog

 

#include <iostream>
#include <cstring>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct treeNode {
	int id;
	char value;
	int left;
	int right;
} treeNode;

//前序遍历二叉树 
void pre_search(int number, treeNode *nodes, int id, string &result) {
	for (int i = 0; i < number; i++) {//对于每一个节点均需搜索节点数组来肯定其在数组中的该节点 
		if (nodes[i].id == id) {
			result.push_back(nodes[i].value);
			if (nodes[i].left != 0)
				pre_search(number, nodes, nodes[i].left, result);//遍历作左节点 
			if (nodes[i].right != 0)
				pre_search(number, nodes, nodes[i].right, result);//遍历右节点 
		}
	}
}
int main() {
	int number;
	while (cin >> number) {
		int id, left, right;
		treeNode *nodes = new treeNode[number];
		//memset(nodes, NULL, sizeof(nodes));
		char value;
		
		for (int i = 0; i < number; i++) {
			cin >> id >> value >> left >> right;
			nodes[i].id = id;
			nodes[i].value = value;
			nodes[i].left = left;
			nodes[i].right = right;
		}
		set<int> rids;
		set<int> cids;
		vector<int> difference;
		int root_id; //根节点的id 
		
		//如下操做为肯定根节点的id,此为本题的关键
		//由于题中为规定根节点为第几个节点,因此因为根节点是不会出如今子节点中的
		//因此分别得到所有节点的id和所有子节点去掉id = 0 后的id,而后在子节点
		//中没有的id,即为根节点id,此处使用了集合set 
		for (int i = 0; i < number; i++) {
			rids.insert(nodes[i].id);
			cids.insert(nodes[i].left);
			cids.insert(nodes[i].right);
		}
		cids.erase(cids.begin()); //集合为自动排序且无重复元素的,此处去除0
		
		for (set<int>::iterator it = rids.begin(); it != rids.end(); it++) {
			if(cids.count(*it) == 0)
				root_id = *it;
		}
		 
		string result;
		pre_search(number, nodes, root_id, result);
		cout << result << endl;
		delete []nodes;
	}
	return 0; 
}
相关文章
相关标签/搜索