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. C#快速排序详解

    使用快速排序法对一列数字进行排序的过程 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists). 步骤为: 从数列中挑出一个元素,称 ...

  2. 邻接表存储图,DFS遍历图的java代码实现

    import java.util.*; public class Main{ static int MAX_VERTEXNUM = 100; static int [] visited = new i ...

  3. POJ2763-Housewife Wind(树链剖分)

    也是入门题,和上一题不一样的是权值在边上. 调了半天后来发现线段树写错了,build的时候没有pushup...蠢哭了好吗.... 做题还是不专心,太慢辣.. #include <algorit ...

  4. EasyUI样式在IE下无法显示原因总结

    1.js css顺序错误 <script type="text/javascript" charset="utf-8" src="js/jque ...

  5. Delphi使用TStringHash实现建立类(有点像反射)

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. JS一定要放在Body的最底部么? 聊聊浏览器的渲染机制

    请参看文章 https://segmentfault.com/a/1190000004292479 网上的回答: 1.js加载会阻塞其它内容加载,使页面加载时间更长,尤其是js文件太大,有的页面js文 ...

  7. PHP 中运用 elasticsearch

    PHP扩展安装 1. 环境要求:PHP_VERSION >= 5.3.9,composer工具 2. 在E盘新建文件夹命名为elastic,,拷贝composer.phar到      E:/e ...

  8. 框架学习笔记:Unity3D的MVC框架——StrangeIoC

    作为从AS3页游走过来的人,看见StrangeIoC会额外亲切,因为StrangeIoC的设计和RobotLegs几乎一致,作为一款依赖注入/控制反转(IoC)的MVC框架,StrangeIoC除了使 ...

  9. OC内存管理基础

    OC 内存管理基础 一. retain和release基本使用 使用注意: 1.你想使用(占用)某个对象,就应该让对象的计数器+1(让对象做一次retain操作) 2.你不想再使用(占用)某个对象,就 ...

  10. Python requests模块在Windows下安装

    发现一个爬虫库太方便了,而且支持python3! 安装方法在http://docs.python-requests.org/en/latest/user/install/#install很详细 只不过 ...