很单纯的树的遍历,但是输入和方向好麻烦!!
下面给出代码,题目来自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的更多相关文章

  1. [Algorithm] Tree Width with Level Width

    // --- Directions // Given the root node of a tree, return // an array where each element is the wid ...

  2. 内存池技术(UVa 122 Tree on the level)

    内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...

  3. 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 ...

  4. 例题6-7 Trees on the level ,Uva122

    本题考查点有以下几个: 对数据输入的熟练掌握 二叉树的建立 二叉树的宽度优先遍历 首先,特别提一下第一点,整个题目有相当一部分耗时在了第一个考查点上(虽然有些不必要,因为本应该有更简单的方法).这道题 ...

  5. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  6. [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, ...

  7. 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, ...

  8. 【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, ...

  9. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

随机推荐

  1. matlab取模与取余

    mod函数采用floor,rem函数采用fix函数.那么什么是floor和fix? fix(x):截尾取整.如: >> fix([3.4 , -3.4]) ans = 3 -3 floor ...

  2. JavaScript(一)---- 概述

    JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...

  3. PHP加水印代码 支持文字和图片水印

    PHP加图片水印.文字水印类代码,PHP加水印类,支持文字图片水印的透明度设置.水印图片背景透明.自己写的一个类,因为自己开发的一套CMS中要用到,网上的总感觉用着不顺手,希望大家也喜欢这个类,后附有 ...

  4. 使用 Redis 统计在线用户人数

    在构建应用的时候, 我们经常需要对用户的一举一动进行记录, 而其中一个比较重要的操作, 就是对在线的用户进行记录. 本文将介绍四种使用 Redis 对在线用户进行记录的方案, 这些方案虽然都可以对在线 ...

  5. windows中如何查看某个端口被谁占用

    说明:本人操作系统为win7 x64,文章转自http://jingyan.baidu.com/article/3c48dd34491d47e10be358b8.html,加上本人的注释. 开始--- ...

  6. 因子分析&主成分分析

    因子分析和主成分分析的异同点: 1.主成分分析仅仅是一种数据变换而不假设数据矩阵有什么样的结构形式 因子分析假定数据有特定的模型,而且齐总的因子满足特定的条件 2.因子分析和主成分分析都是从相关矩阵出 ...

  7. 让表格table呈现边框,不用给tr、td加边框的写法

    <table width="100%" cellspacing="1" cellpadding="1" bgcolor="# ...

  8. [bzoj2120][数颜色] (暴力 or 分块)

    Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜 ...

  9. 用js来实现页面的换肤功能(带cookie记忆)

    用js来实现页面的换肤功能 js实现换肤功能的实现主要是通过利用js控制CSS来实现的.大致的实现原理是这样的, 1.先定义一个页面基本样式style.css来确定div的宽高等属性,使得整个页面的D ...

  10. MyBatis动态SQL与模糊查询

    sqlxml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...