很单纯的树的遍历,但是输入和方向好麻烦!!
下面给出代码,题目来自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. Maven的安装环境配置

    一.Maven的安装 二.Maven的配置 Settings.xml可以用来定义本地仓库.远程仓库.联网代理 Settings.xml文件可以存在两个地方: 1.多用户情况 conf目录下 2.单用户 ...

  2. 利用curl验证ssl网站(webservice)

    curl的用法: http://linux.about.com/od/commands/l/blcmdl1_curl.htm DER格式的cert转换为PEM格式(curl只接受PEM格式): ope ...

  3. adapter中报错:Can't create handler inside thread that has not called Looper.prepare()

    http://stackoverflow.com/questions/9357513/cant-create-handler-inside-thread-that-has-not-called-loo ...

  4. css清除浮动大全,共8种方法

    我看的网页的网址:http://hi.baidu.com/kongcheng2012/item/2b1250d4452e802538f6f705 为什么浮动这么难? 因为浮动会使当前标签产生向上浮的效 ...

  5. 高尔夫 之 TEE

    黑色:男子职业选手蓝色:男子业余高手.女子职业选手白色:男子初学球手.老年球手.女子业余高手红色:女子选手金色:有的球场做为介于 黑Tee (职业球员)与 蓝Tee (业余高手)之间的Tee 台:有的 ...

  6. error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches

    error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches &quo ...

  7. 浅谈ServletContext

    ServletContext是什么? WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用.ServletConfig对象中维护了Serv ...

  8. Brackets - 强大免费的开源跨平台Web前端开发工具IDE (HTML/CSS/Javascript代码编辑器)

    Brackets 是一个免费.开源且跨平台的 HTML/CSS/JavaScript 前端 WEB 集成开发环境 (IDE工具).该项目由 Adobe 创建和维护,根据MIT许可证发布,支持 Wind ...

  9. TIMESTAMP和DATETIME哪个好

    日期范围 TIMESTAMP 支持从'1970-01-01 00:00:01′ 到 '2038-01-19 03:14:07′ UTC. 这个时间可能对目前正在工作的人来说没什么问题,可以坚持到我们退 ...

  10. SpringMVC 前端获得定义JSON对象的方法

    SpringMVC 前端获得定义JSON对象的方法: 可以使用map进行对象的创建,这样就会解析成键值对,不需要为前端专门定义对象.