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. 重新执笔,已是大三!Jekyll自定义主题开发

    前言 “一转眼忘了时间 丢了感觉 黑了世界 再逞强 再疯狂 也会伤 不知 不觉 后知 后觉 然后 发现 失去 知觉 ”——<一吻不天荒> 感言 时间是把双刃剑,什么解决不了,忧烦的,慢慢变 ...

  2. [HIve - LanguageManual] Joins

    Hive Joins Hive Joins Join Syntax Examples MapJoin Restrictions Join Optimization Predicate Pushdown ...

  3. Spark系列(十)TaskSchedule工作原理

    工作原理图 源码分析: 1.) 25              launchedTask = true 26            } 27          } catch { 28         ...

  4. android中向bitmap里写入文字

    public static Bitmap drawText2Bitmap(String text, int imgResourceId, Context mContext) { try { Resou ...

  5. 第二百九十三天 how can I 坚持

    总感觉怪怪的,换了个领导,好烦,虽然对我没用影响. 其实,还是智商低,不懂人情世故,就像...算了,不说了,只能当自己傻. 最近好冷啊,十年不遇的寒冬. 心情有些压抑. 不玩游戏了,看了集康熙来了.小 ...

  6. JS代码格式化修改表格的数值的格式

    今天在cognos中第一次需要用到JS,主要是报表页面展示的时候是可能得到如下的数据 ,我需要对其中类型中有金额字样的,后面的数值,精确2位小数:有百分比字样的,数值显示成百分比.如下. 我先尝试了自 ...

  7. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  8. json网页预览插件

  9. [html]html常用代码

    上传文件表单属性 enctype="multipart/form-data" 单选(是否选中) checked="checked" 下拉列表(是否选中) sel ...

  10. hdu 5745 La Vie en rose DP + bitset优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...