Tree on the level UVa122
很单纯的树的遍历,但是输入和方向好麻烦!!
下面给出代码,题目来自UVa 122
#include<cstdio>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<queue>
using namespace std;
+ ;
struct Node{
bool have_value;
int v;
Node *right,*left;
Node():have_value(false),right(NULL),left(NULL){}
};
Node *root;
Node* newnode(){return new Node();}
void remove_tree(Node* u){ //防止内存泄露。 但可能会出现内存碎片
if(u == NULL)return;
remove_tree(u->left);
remove_tree(u->right);
delete u;
}
char s[maxn];
bool failed;
void addnode(int v,char *s){
int len = strlen(s);
Node* u = root;
; i < len;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(;;){
) return false;
if(!strcmp(s,"()"))break; //记住!!strcmp函数中的第二个参数是字符串!!
int v;
sscanf(&s[],"%d", &v); //&s[1]所对应的字符串是从s[1]后的字符串
addnode(v,strchr(s,); //函数strchr(s,',')返回字符串s中从左往右第一个字符","的指针
}
return true;
}
bool bfs(vector<int>& ans){ //bfs遍历树
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(){
freopen("tree.txt","r",stdin);
freopen("treeout.txt","w",stdout);
vector<int> ans;
while(read_input()){
; // 参数!!
if(failed)printf("-1\n");
else{
; i < ans.size();i++){
if(i)printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
}
;
}
Tree on the level UVa122的更多相关文章
- [Algorithm] Tree Width with Level Width
// --- Directions // Given the root node of a tree, return // an array where each element is the wid ...
- 内存池技术(UVa 122 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...
- Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vect ...
- 例题6-7 Trees on the level ,Uva122
本题考查点有以下几个: 对数据输入的熟练掌握 二叉树的建立 二叉树的宽度优先遍历 首先,特别提一下第一点,整个题目有相当一部分耗时在了第一个考查点上(虽然有些不必要,因为本应该有更简单的方法).这道题 ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [Leetcode][JAVA] Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- leetcode 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
随机推荐
- C# 调用外部dll(转)
C# 调用外部dll 一. DLL与应用程序 动态链接库(也称为DLL,即为"Dynamic Link Library"的缩写)是Microsoft Windows最 ...
- jade的特点
jade特点 1超强的可读性 2灵活易用的缩进 3块扩展 4代码默认进过编码处理,以增强安全性 5编译及运行时的上下文错误报告 6命令行编译支持 7html5模式(使用 !!!5文档类型) 8可选的内 ...
- javascript中的字典
1.概念 字典是一种以键值对的形式存储的数据结构,就系那个电话本中的名字和电话号码一样.要找到一个电话首先要找到名字,再根据名字找到电话号码.这里的键就是指用来查找的东西,值就是查找得到的结果. Ja ...
- div垂直居中(js)
window.onload = function(){ var xx = document.documentElement.clientHeight; // 470为要垂直居中的div的高度 he = ...
- 创建 github 仓库
1. 创建入口 在右上角找到 “+” 然后,选择 “New repository” 进行创建. 2. 填入信息 输入名字和描述 . 选择 “Initialize this repository wit ...
- 如何在Windows Server 2016启用或关闭Internet Explorer增强的安全配置
一般我们安装完服务器后,开启 Internet Explorer 会发现无法上网或者上网内容被屏蔽掉了 问题的发生原因 在 Windows Server 2016 通常扮演重要的服务器角色,不应该用来 ...
- Repeated Substring Pattern Leetcode
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- win32 安装 xcache扩展
今天整了一上午,才整明白. 我的系统是是 win7 32位 ,用的环境是 Wamp,php版本是5.3.5,后来在http://xcache.lighttpd.net/pub/Releases/3.0 ...
- HDU-5123-who is the best?
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5123 水题一个,直接hash: 代码 #include<stdio.h>#include& ...
- 强行在MFC窗体中渲染Cocos2d-x 3.6
[前言] 把Cocos2dx渲染到另一个应用程序框架中的方法,在2.x时代有很多大神已经实现了,而3.x的做法网上几乎找不着.这两天抽空强行折腾了一下,不敢独享,贴出来供大家参考. [已知存在的问题] ...