zoukankan      html  css  js  c++  java
  • UVa 122 Trees on the level(链式二叉树的建立和层次遍历)

    题目链接:

    https://cn.vjudge.net/problem/UVA-122

      1 /*
      2 问题
      3 给出每个节点的权值和路线,输出该二叉树的层次遍历序列。
      4 
      5 解题思路
      6 根据输入构建链式二叉树,再用广度优先遍历保存权值最后输出。 
      7 */ 
      8 #include<cstdio>
      9 #include<cstring>
     10 #include<vector>
     11 #include<queue>
     12 using namespace std;
     13 const int maxn=110;
     14 bool failed;
     15 
     16 struct NODE{
     17     bool have_value;
     18     int v;
     19     NODE *left,*right;
     20     NODE() : have_value(false),left(NULL),right(NULL){};
     21 };
     22 NODE* newnode(){
     23     return new NODE();
     24 }
     25 NODE* root;
     26 
     27 bool read_input();
     28 void addnode(int v,char *s);
     29 bool bfs(vector<int> &ans);
     30 void remove_tree(NODE* u){
     31     if(u == NULL) return;
     32     remove_tree(u->left);
     33     remove_tree(u->right);
     34     delete u;
     35 }
     36 
     37 int main()
     38 {
     39     //freopen("E:\testin.txt","r",stdin);
     40     vector<int> ans;
     41     while(read_input()){
     42         if(failed || !bfs(ans))
     43             printf("not complete
    ");
     44         else{
     45             int i;
     46             for(i=0;i<ans.size()-1;i++)
     47                 printf("%d ",ans[i]);
     48             printf("%d
    ",ans[i]); 
     49         }
     50     }
     51     return 0;
     52 }
     53 
     54 bool bfs(vector<int> &ans){
     55     queue<NODE*> q;
     56     ans.clear();
     57     q.push(root);
     58     
     59     while(!q.empty()){
     60         NODE* u =q.front(); q.pop();
     61         if(!u->have_value)    return false;
     62         ans.push_back(u->v);
     63         
     64         if(u->left != NULL)    q.push(u->left);
     65         if(u->right != NULL) q.push(u->right);
     66     }
     67     return true;
     68 }
     69 void addnode(int v,char *s){
     70     int len=strlen(s);
     71 
     72     NODE* u= root;
     73     for(int i=0;i<len;i++){
     74         if(s[i] == 'L'){
     75             if(u->left == NULL) 
     76                 u->left= newnode();
     77             u=u->left;        
     78         }else if(s[i] == 'R'){
     79             if(u->right == NULL) 
     80                 u->right= newnode();
     81             u=u->right;
     82         }
     83     }
     84     
     85     if(u->have_value)    failed=true;
     86     u->v= v;
     87     u->have_value=true;
     88 }
     89 bool read_input(){
     90     char s[maxn];
     91     failed=false;
     92     remove_tree(root);
     93     root=newnode();
     94     for(;;){
     95         if(scanf("%s",s) != 1)    return false;
     96         if(!strcmp(s,"()"))    break;
     97         int v;
     98         sscanf(s+1,"%d",&v);
     99         addnode(v,strchr(s,',')+1);
    100     }
    101     return true;
    102 }
  • 相关阅读:
    spring3.1, hibernate4.1 配置备份,struts2.2.1,sitemesh 2.4.2
    java 动态AOP
    制作可以执行的 JAR 文件包及 jar 命令详解
    struts result Annotation 参考
    Android线段与矩形碰撞检测函数
    防止aspxspy木马列服务 iis信息 执行命令提权等操作
    博客园申请及页面定制CSS
    C# 中将月份格式化为英语缩写格式
    通过Web Service获取天气预报并朗读
    windows下html/javascript调用可执行程序
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/9129763.html
Copyright © 2011-2022 走看看