Trees on the level(指针法和非指针法构造二叉树)
Trees on the level
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 584 Accepted Submission(s): 195
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622
This problem involves building and traversing binary trees.
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.
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.
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()
not complete
题意: 输入一棵二叉树,按照从上到下从左到右的顺序输出各个节点的值,每个节点都是按照从根节点到他的移动的序列给出的(L表示左,R表示右)在输入中,每个编号左括号和右括号之间没有空格,相邻的节点之间用一个空格隔开,每棵树的输入用括号()表示结束,这个括号本身不代表一个节点,注意,当根到某个叶节点的路径上的节点没有在输入中给出,或者给出超过一次,输出not complete 节点个数不超过256
题解:学习紫书上的代码,给出完整的用指针和用数组的方法的代码
一、用指针建树:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define maxn 260
char s[maxn];
struct Node{
bool have_value;//是否被赋值过
int v;
Node *left,*right;
Node():have_value(false),left(NULL),right(NULL){}//构造函数
};
Node *root; Node* newnode(){
return new Node();
} bool failed; void remove_tree(Node *u){//防止内存泄漏
if(u == NULL) return;
remove_tree(u->left);
remove_tree(u->right);
delete(u);
} void addnode(int v, char* s){
int n = strlen(s);
Node* u = root;
for(int i = ; i < n; i++){
if(s[i]=='L'){
if(u->left == NULL) u->left = newnode();
u = u->left;
}else if(s[i] == 'R'){
if(u->right == NULL) u->right = newnode();
u = u->right;
}
}
if(u->have_value) failed = true;//已经赋值过的认为是错误的输入
u->v = v;
u->have_value = true;//记得做标记
} bool read_input(){
failed = false;
remove_tree(root);//释放掉之前建立过得树
root = newnode();
for(;;){
if(scanf("%s",s)!=) return false;//整个输入结束
if(!strcmp(s,"()")) break;//读到结束标志
int v;
sscanf(&s[],"%d",&v);//读入节点值
addnode(v,strchr(s,',')+);//查找逗号,然后插入节点
}
return true;
} vector< int > ans; bool bfs(vector<int> & ans){
queue<Node*> q;
ans.clear(); q.push(root);
while(!q.empty()){
Node* u = q.front(); q.pop();
if(!u->have_value) return false;
ans.push_back(u->v);
if(u->left != NULL) q.push(u->left);
if(u->right != NULL) q.push(u->right);
}
return true;
} int main()
{
while(read_input()==true){
if(!failed&&bfs(ans)){
vector<int>::iterator it;
for(it = ans.begin(); it != ans.end()-; it++)
{
printf("%d ", (*it));
}
printf("%d\n",(*it));
}
else puts("not complete");
}
return ;
}
二、用数组代码:
注意,就算是用数组也要先建立路径中的节点
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#define N 260
const int root = ; char s[N]; int left[N];
int right[N];
bool have_value[N];
int val[N]; int cnt;
void newtree(){ left[root] = right[root] = ; have_value[root] = false; cnt = root; }
int newnode(){ int u = ++cnt; left[u] = right[u] = ; have_value[u] = false; return u; } bool failed; void addnode(int v, char* s){
int n = strlen(s);
// int u = newnode();
int tm = root;
for(int i = ; i < n; i++){
if(s[i]=='L'){
if(left[tm]==) left[tm] = newnode();
tm = left[tm];
}else if(s[i] == 'R'){
if(right[tm]==) right[tm] = newnode();
tm = right[tm];
}
}
if(have_value[tm]) failed = true;//已经赋值过的认为是错误的输入
val[tm] = v;
have_value[tm] = true;//记得做标记
} bool read_input(){
failed = false;
newtree();
for(;;){
if(scanf("%s",s)!=) return false;//整个输入结束
if(!strcmp(s,"()")) break;//读到结束标志
int v;
sscanf(&s[],"%d",&v);//读入节点值
addnode(v,strchr(s,',')+);//查找逗号,然后插入节点
}
return true;
} std::vector< int > ans; bool bfs(std::vector<int> & ans){
std::queue<int> q;
ans.clear(); q.push(root);
while(!q.empty()){
int u = q.front(); q.pop();
if(!have_value[u]) return false;
ans.push_back(val[u]);
if(left[u] != ) q.push(left[u]);
if(right[u] != ) q.push(right[u]);
}
return true;
} using namespace std;
int main()
{
while(read_input()==true){
if(!failed&&bfs(ans)){
vector<int>::iterator it;
for(it = ans.begin(); it != ans.end()-; it++)
{
printf("%d ", (*it));
}
printf("%d\n",(*it));
}
else puts("not complete");
}
return ;
}
Trees on the level(指针法和非指针法构造二叉树)的更多相关文章
- synchronized 修饰在 static方法和非static方法的区别
Java中synchronized用在静态方法和非静态方法上面的区别 在Java中,synchronized是用来表示同步的,我们可以synchronized来修饰一个方法.也可以synchroniz ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- Java中synchronized 修饰在static方法和非static方法的区别
[问题描述]关于Java中synchronized 用在实例方法和对象方法上面的区别 [问题分析]大家都知道,在Java中,synchronized 是用来表示同步的,我们可以synchronized ...
- E - Trees on the level
Trees on the level Background Trees are fundamental in many branches of computer science. Current ...
- VB.NET 内存指针和非托管内存的应用
介绍 Visual Basic 从来不像在C或C++里一样灵活的操纵指针和原始内存.然而利用.NET框架中的structures 和 classes,可以做许多类似的事情.它们包括 IntPtr, ...
- java——多线程——单例模式的static方法和非static方法是否是线程安全的?
单例模式的static方法和非static方法是否是线程安全的? 答案是:单例模式的static方法和非static方法是否是线程安全的,与单例模式无关.也就说,如果static方法或者非static ...
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- hdu 1622 Trees on the level(二叉树的层次遍历)
题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...
随机推荐
- 【Zookeeper】源码分析之服务器(一)
一.前言 前面已经介绍了Zookeeper中Leader选举的具体流程,接着来学习Zookeeper中的各种服务器. 二.总体框架图 对于服务器,其框架图如下图所示 说明: ZooKeeperServ ...
- php实现socket推送技术
在socket出现之前已经有ajax定时请求.长轮询等方案,但都不能满足需求,socket就应用而生了. socket基本函数socket 总结下常用的socket函数 服务端: socket_cre ...
- 【WebGL】《WebGL编程指南》读书笔记——第4章
一.前言 今天继续第四章的学习内容,开始学习复合变换的知识. 二.正文 Example1: 复合变换 在书中,作者为我们封装了一套用于变换的矩阵对象:Matrix4对象.它 ...
- 阅读MDN文档之CSS选择器介绍(一)
本文为阅读MDN文档笔记 目录 Different types of Selectors Attribute Selectors Presence and value attribute select ...
- touch监听判断手指的上滑,下滑,左滑,右滑,事件监听
判断滑动的方向和距离,来实现一定的效果,比如返回上一页等等 <body> <script> $(function(){ //给body强制定义高度 var windowHeig ...
- MySQL主从复制原理以及架构
1 复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的 数据复制到其它主机(slaves)上,并 ...
- 自动删除文件脚本(Linux shell脚本)
每天在/home/face/capturepic/2017/目录下都会产生很多文件 /home/face/capturepic/2017/4/21 /home/face/capturepic/2017 ...
- Docker三十分钟快速入门(上)
一.背景 最近,Docker技术真是一片火热,它的出现也弥补了虚拟机资源消耗过高的问题,直接让虚拟化技术有了质的飞跃.那么本文我们来聊一聊Docker,和大家一起认识Docker,简单入门Dock ...
- Linux常见命令(权限)
创建a.txt和b.txt文件,将他们设为其拥有者和所在组可写入,但其他以外的人则不可写入:chmod ug+w,o-w a.txt b.txt 创建c.txt文件所有人都可以写和执行chmod a= ...
- 8086cpu中的标志寄存器与比较指令
在8086CPU中有一个特殊的寄存器--标志寄存器,该寄存器不同于其他寄存器,普通寄存器是用来存放数据的读取整个寄存器具有一定的含义,但是标志寄存器是每一位都有固定的含义,记录在运算中产生的信息,标志 ...