sicily 1156. Binary tree

来源:互联网 发布:linux使用 编辑:程序博客网 时间:2024/05/01 16:57

题目描述:
Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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.

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

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

解题思路:
一个简单的前序遍历,问题的重点在于如何建树,

  1. 如果建树也可以,应该可以用hash把树和节点值对应起来,就可以建树了,但由于多个测试样例,因此每次循环完要注意删除树,否则前一个测试样例的结果会影响后一个的结果,删除树用后续遍历,对于一道算法题来说,这种实现太繁琐了,所以不采用这种做法。
  2. 不建立树的话,问题的重点在于如何找到树根,下面是一种思路
#include <cstdio>#include <set>#include <cstring>using namespace std;struct node{    char content;    int left;    int right;};void preOrder(const node* tree, int root){    if(root == 0)        return;    printf("%c", tree[root].content);    preOrder(tree, tree[root].left);    preOrder(tree, tree[root].right);}int main(){    int n, identifier, left, right;    char content;    node tree[1009];    memset(tree, 0, sizeof(tree));    while(scanf("%d", &n) != EOF){        bool isChild[1009];        memset(isChild, 0, sizeof(isChild));        set<int> nd;        for(int i = 0; i < n; i++){            scanf("%d %c %d %d", &identifier, &content, &left, &right);            nd.insert(identifier);            nd.insert(left);            nd.insert(right);            isChild[left] = isChild[right] = 1;            tree[identifier].content = content;            tree[identifier].left = left;            tree[identifier].right = right;        }        int root = 0;        for(set<int>::iterator it = nd.begin(); it != nd.end(); it++)            if(!isChild[*it]){                root = *it;                break;            }        preOrder(tree, root);        printf("\n");    }    return 0;}

总的复杂度为O(n),空间也是O(n),在找根的过程这里使用set来做,当然,这道题目中的数据只有1000,开两个1000的数组保留有使用的节点和保留非根节点,然后遍历这个数组同样也可以得到根。

1 0
原创粉丝点击