sicily 1156.Binary tree

1156. Binary tree

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) 调用memset的头文件是memory.h 而不是
(2) a[100]={1}是没用的,必须用memset对整个数组赋值或者循环赋值
(3) 先序遍历树之前,一定要判断左节点或者右节点是否为空,若空的话就不要继续遍历
(4) id数组可以作为一个中间映射,来实现把非连续的index映射到连续的index上,方便循环判断

代码见下:
// Problem#: 1156
// Submission#: 3017260
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <stack>
#include <iterator>
#include <memory.h> // 注意是<memory.h> 而不是 <memory>
using namespace std;

struct node
{
    char c;
    int left, right;
};
node tree[1005];
void pre(node n)
{
    cout << n.c;
    if (n.left != 0)    // 这句话很关键,因为如果是0的话,就是空子树,会出错的
        pre(tree[n.left]);
    if (n.right != 0)
        pre(tree[n.right]);
}

int main()
{
    int T;
    
    while (cin >> T)
    {
        int isRoot[1005], k = 0, id[1005];
        memset(isRoot, 1, sizeof(isRoot));
        while (T--)
        {
            int n, l, r;
            char e;
            cin >> n >> e >> l >> r;
            tree[n].c = e;
            tree[n].left = l;
            tree[n].right = r;
            isRoot[l] = isRoot[r] = 0;
            id[k] = n;  // id数组用于映射index和n,来找到根节点
            k++;
        }
        for (int i = 0; i < k; i++) // 寻找根节点
        {
            if (isRoot[id[i]])
            {
                pre(tree[id[i]]);
                cout << endl;
                break;      // 减少运行时间
            }
        }
    }
    return 0;
}                                 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值