zoukankan      html  css  js  c++  java
  • (二叉树)UVA

    学到很多的题目。

    利用ssrand使处理数据更加方便,动态管理内存的做法也值得学习。

      1 #include <iostream>
      2 #include <string>
      3 #include <algorithm>
      4 #include <cstring>
      5 #include <cstdio>
      6 #include <cmath>
      7 #include <queue>
      8 #include <set>
      9 #include <map>
     10 #include <list>
     11 #include <stack>
     12 #define mp make_pair
     13 typedef long long ll;
     14 typedef unsigned long long ull;
     15 const int MAX=300;
     16 const int INF=1e9+5;
     17 using namespace std;
     18 typedef pair<int,int> pii;
     19 char temp[MAX];
     20 bool st;
     21 int an[MAX],ge;
     22 struct Node
     23 {
     24     bool vi;
     25     int value;
     26     Node *left,*right;
     27     Node():vi(false),left(NULL),right(NULL){}
     28 };
     29 Node *root;
     30 Node *newnode()
     31 {
     32     return new Node();
     33 }
     34 void addnode(int val,char *s)
     35 {
     36     int len=strlen(s);
     37     Node *u=root;
     38     for(int i=0;i<len-1;i++)
     39     {
     40         if(s[i]=='L')
     41         {
     42             if(u->left==NULL)
     43                 u->left=newnode();
     44             u=u->left;
     45         }
     46         else if(s[i]=='R')
     47         {
     48             if(u->right==NULL)
     49                 u->right=newnode();
     50             u=u->right;
     51         }
     52     }
     53     if(u->vi)
     54         st=false;
     55     u->value=val;
     56     u->vi=true;
     57 }
     58 bool bfs()
     59 {
     60     ge=0;
     61     queue<Node *>que;
     62     que.push(root);
     63     while(!que.empty())
     64     {
     65         Node *tem=que.front();que.pop();
     66         if(!tem->vi)
     67             return false;
     68         an[++ge]=tem->value;
     69         if(tem->left!=NULL)
     70             que.push(tem->left);
     71         if(tem->right!=NULL)
     72             que.push(tem->right);
     73     }
     74     return true;
     75 }
     76 void remove_tree(Node *u)
     77 {
     78     if(u==NULL)
     79         return;
     80     remove_tree(u->left);remove_tree(u->right);
     81     delete u;
     82 }
     83 int main()
     84 {
     85     while(1)
     86     {
     87         st=true;root=newnode();
     88         while(1)
     89         {
     90             if(scanf("%s",temp)!=1)
     91                 exit(0);
     92             if(!strcmp(temp,"()"))
     93                 break;
     94             int v;
     95             sscanf(&temp[1],"%d",&v);
     96             addnode(v,strchr(temp,',')+1);
     97         }
     98         if(bfs()&&st)
     99         {
    100             for(int i=1;i<=ge;i++)
    101             {
    102                 if(i!=1)
    103                     printf(" ");
    104                 printf("%d",an[i]);
    105             }
    106         }
    107         else
    108             printf("not complete");
    109         printf("
    ");
    110         remove_tree(root);
    111     }
    112 }
  • 相关阅读:
    webpack中设置jquery为全局对象
    JS判断不同web访问环境,主要针对移动设备,
    js比较两个日期天数差
    原生js跨域
    我们项目中用到的jsonp跨域
    Js跨域解决方法总结
    js call的使用,js call 方法实现继承
    windows下配置bower路径
    兼容弹层代码
    自定义下拉列表框(2015.1.30)
  • 原文地址:https://www.cnblogs.com/quintessence/p/6715925.html
Copyright © 2011-2022 走看看