很单纯的树的遍历,但是输入和方向好麻烦!!
下面给出代码,题目来自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. 【noip】华容道

    描述 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间. 小 B 玩的华容道与经典的 ...

  2. svn无法提交

    svn无法提交, 错误信息:Commit failed. svn: E200007: CHECKOUT can only be performed on a version resource... 解 ...

  3. 获取IE浏览器关闭事件

    //关闭浏览器时才会触发此操作,刷新页面不执行 //n 检测鼠标相对于用户屏幕的水平位置 - 网页正文部分左:求出鼠标在当前窗口上的水平位置(参照:当前窗口右上角为0.0坐标) //m 网页正文全文宽 ...

  4. Html 定位position

    CSS position属性和实例应用   目前几乎所有主流的浏览器都支持position属性("inherit"除外,"inherit"不支持所有包括IE8和 ...

  5. linux环境下Vim的配置

    原文链接:http://blog.chinaunix.net/uid-26826958-id-3272375.html  (本文转自此链接中的部分内容,但做了适当修改) 安装vim命令:sudo ap ...

  6. PHP连接和拆分数组array_combine()和array_slice()用法示例

    一提起数组,可能很多PHP初学者会觉得难,但开发一些高级应用的时候,又离不开数组的使用.下面就来说下,PHP使用array_combine()函数来连接数组.用array_slice()函数来拆分数组 ...

  7. C++第三天学习

    回顾: 类和对象 class 类名 { public: private: 在类的类部可以访问,在类的外部不能直接访问. protected: 在类的类部可以访问,在类的外部不能直接访问,其子类可以访问 ...

  8. Spring @Aspect实现切面编程

    参考:http://blog.csdn.net/cdl2008sky/article/details/6268628 参考:http://www.360doc.com/content/12/0602/ ...

  9. easyUI tootip组件使用

    easyUI tootip组件使用: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  10. Canvas贝塞尔二级曲线

    当前点到控制点,控制点到末尾点的两个连线,与这两个连线相切:<!DOCTYPE html> <html lang="en"> <head> &l ...