Trees on the level 

Background

Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics.

This problem involves building and traversing binary trees.

The Problem

Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes.

In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1.

For example, a level order traversal of the tree

is: 5, 4, 8, 11, 13, 4, 7, 2, 1.

In this problem a binary tree is specified by a sequence of pairs (n,s) where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of L's and R's where L indicates a left branch and R indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.

The Input

The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs (n,s) as described above separated by whitespace. The last entry in each tree is (). No whitespace appears between left and right parentheses.

All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file.

The Output

For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ``not complete'' should be printed.

Sample Input

(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()

Sample Output

5 4 8 11 13 4 7 2 1
not complete
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
const int INF = 0x7fffffff;
const double EXP = 1e-;
const int MS = ;
struct node
{
string value;
string path;
//node(string v = "", string pa = "") :value(va), path(pa){}
bool operator < (const node &b)
{
if (path.length() != b.path.length())
return path.length() < b.path.length();
return path < b.path;
}
}nodes[MS];
map<string, int> m1, m2;
int get_comma(string &s)
{
int len = s.length();
for (int i = ; i < len; i++)
if (s[i] == ',')
return i;
} int main(int argc, char *argv[])
{
string str;
bool flag = true;
int cnt = ;
ios_base::sync_with_stdio(false);
while (cin >> str)
{
if (str != "()")
{
int i = get_comma(str);
nodes[cnt].value = str.substr(, i - );
nodes[cnt].path = str.substr(i + , str.length() - i - );
if (m1[nodes[cnt].path]) //m1.count(nodes[cnt].path)!=0
flag = false;
else
m1[nodes[cnt].path] = ;
cnt++;
}
else
{
if (flag)
{
sort(nodes, nodes + cnt);
if (nodes[].path.length() == ) //可能没有根节点
{
m2[nodes[].path] = ;
for (int i = ; i < cnt&&flag; i++)
{
if (m2[nodes[i].path.substr(, nodes[i].path.length() - )] == )
flag = false;
else
m2[nodes[i].path] = ;
}
}
else
flag = false;
}
if (flag)
for (int i = ; i < cnt; i++)
{
if (i)
cout << " ";
cout << nodes[i].value;
}
else
cout << "not complete";
cout << endl;
m1.clear();
m2.clear();
cnt = ;
flag = true;
}
}
return ;
}

E - Trees on the level的更多相关文章

  1. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

  3. UVA.122 Trees on the level(二叉树 BFS)

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

  4. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

  5. UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  6. uva 122 trees on the level——yhx

    题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...

  7. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

  8. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

  9. Trees on the level (二叉链表树)

    紫书:P150 uva122 Background Trees are fundamental in many branches of computer science. Current state- ...

随机推荐

  1. 8个很有用的PHP安全函数,你知道几个?

    原文:Useful functions to provide secure PHP application 译文:有用的PHP安全函数 译者:dwqs 安 全是编程非常重要的一个方面.在任何一种编程语 ...

  2. 2.2……测试

    概述 黑盒测试: 是以用户的角度,从输入数据与输出数据的对应关系出发进行测试的.   白盒测试: 又称结构测试.透明盒测试.逻辑驱动测试或基于代码的测试.   单元测试: 又称模块测试,是开发者编写的 ...

  3. 分析特定类的python脚本

    今天接触了下pyUSB,事先没看对象内部成员资料,直接用python的dir函数看了看pyUSB的内部构成.突然间想到自己可不可以写个简单的脚本,利用dir或其他函数遍历某个对象内部的所有成员,并打印 ...

  4. Java学习笔记(3)

    “当你定义出一组类的父型时,你可以用子型的任何类来填补任何需要或期待父型的位置” “运用多态时,引用类型可以是实际对象类型的父类”Animal myDog = new Dog(); 三种方法可以防止某 ...

  5. 一起刷LeetCode3-Longest Substring With Repeating Characters

    拖延症太严重了TAT,真心要处理一下这个问题了,感觉很不好! --------------------------------------------------------------------- ...

  6. Navicate

    快捷键 1.ctrl+q 打开查询窗口 2.ctrl+/ 注释sql语句 3.ctrl+shift +/ 解除注释 4.ctrl+r 运行查询窗口的sql语句 5.ctrl+shift+r 只运行选中 ...

  7. 新版本ffmpeg解码非完整H264帧失败

    按照ffmpeg/doc/examples/decoding_encoding.c中video_decode_example解码H264,新版本ffmpeg解码非完整H264帧,定量读取数据直接给av ...

  8. typedef typedef struct的使用

    typedef通常情况用于声明结构体之类的 1,定义某些便于记忆的结构体或者使现有的类型看上去更加整齐,比如后来因为经常使用而被添加进入c/c++标准头文件的stdint.h typedef sign ...

  9. 【转】使用JavaScriptCore在JS和OC间通信

    http://www.cocoachina.com/ios/20160623/16796.html iOS 开发中,我们时不时的需要加载一些 Web 页面,一些需求使用 Web 页面来实现可以更可控, ...

  10. 【C语言】-一维数组

    数组: 数组是一系列相同类型的有序数据的集合,数组中的每一个元素都是同一个数据类型,所有元素共用一个名字,用下标来区别数组中的每一个元素. C语言中,数组属于构造数据类型.一个数组中含有多个数组元素, ...