zoukankan      html  css  js  c++  java
  • UVA 122 Trees on the level (二叉树的层次遍历)

    题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点。

    分析:先建树,建树的过程中,沿途结点都申请了内存,所以在bfs的时候如果该结点有内存,但是未赋值,那就算not complete,别忘了释放内存,虽然这不影响AC,但是注意内存泄漏是好习惯。

    PS:

    strchr函数原型:char * strchr(char * str, int ch); 功能就是找出在字符串str中第一次出项字符ch的位置,找到就返回该字符位置的指针(也就是返回该字符在字符串中的地址的位置),找不到就返回空指针(就是 null)。
    strstr 函数原型: char * strstr(char * str1,char * str2);功能就是找出在字符串str1中第一次出项字符串str2的位置(也就是说字符串sr1中要包含有字符串str2),找到就返回该字符串位置的指针(也就是返回字符串str2在字符串str1中的地址的位置),找不到就返回空指针(就是 null)。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    typedef long long ll;
    typedef unsigned long long llu;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
    const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1};
    const int dc[] = {-1, 1, 0, 0};
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    const int MAXN = 300 + 10;
    const int MAXT = 1000000 + 10;
    using namespace std;
    char s[MAXN];
    bool ok;
    vector<int> a;
    struct Node{
        Node* left;
        Node* right;
        int value;
        bool flag;//结点是否被赋值过
        Node():left(NULL), right(NULL), flag(false){}
    };
    Node* root;
    void addNode(int v, char* s){
        Node *u = root;
        int len = strlen(s) - 1;
        for(int i = 0; i < len; ++i){
            if(s[i] == 'L'){
                if(u -> left == NULL){
                    u -> left = new Node();
                }
                u = u -> left;
            }
            else{
                if(u -> right == NULL){
                    u -> right = new Node();
                }
                u = u -> right;
            }
        }
        if(u -> flag == true){
            ok = false;//某结点超过一次被赋值
        }
        else{
            u -> flag = true;
            u -> value = v;
        }
    }
    void bfs(){
        queue<Node*> q;//因为用指针遍历的,所以加*
        if(root != NULL){
            if(root -> flag){
                q.push(root);
            }
            else{
                ok = false;
                return;
            }
        }
        while(!q.empty()){
            Node *tmp = q.front();
            a.push_back(tmp -> value);
            q.pop();
            if(tmp -> left != NULL){
                if(tmp -> left -> flag)
                    q.push(tmp -> left);
                else{
                    ok = false;
                    return;
                }
            }
            if(tmp -> right != NULL){
                if(tmp -> right -> flag)
                    q.push(tmp -> right);
                else{
                    ok = false;
                    return;
                }
            }
        }
    }
    void delete_tree(Node *root){
        if(root == NULL) return;
        if(root -> left != NULL) delete_tree(root -> left);
        if(root -> right != NULL) delete_tree(root -> right);
        delete root;
    }
    int main(){
        while(scanf("%s", s) == 1){
            ok = true;
            a.clear();
            if(!strcmp(s, "()")){
                printf("not complete\n");
                return 0;
            }
            int v;
            sscanf(s + 1, "%d", &v);//第一个参数必须是该整数在此字符串中第一次出现的位置,v中存的是s中第一个出现的整数
            delete_tree(root);
            root = new Node();
            addNode(v, strchr(s, ',') + 1);//返回字符','在字符串s中的地址的位置,找不到则返回null
            while(scanf("%s", s) == 1){
                if(!strcmp(s, "()")) break;
                sscanf(s + 1, "%d", &v);
                addNode(v, strchr(s, ',') + 1);
            }
            bfs();
            if(!ok) printf("not complete\n");
            else{
                int len = a.size();
                for(int i = 0; i < len; ++i){
                    if(i) printf(" ");
                    printf("%d", a[i]);
                }
                printf("\n");
            }
        }
        return 0;
    }
  • 相关阅读:
    Chrome 已经原生支持截图功能,还可以给节点截图!
    【promise| async/await】代码的控制力
    移动端各种分辨率手机屏幕----适配方法集锦
    Web Storage事件无法触发
    【php学习】图片处理三步走
    NYOJ 36 LCS(最长公共子序列)
    NYOJ 252 01串 普通dp
    NYOJ 18 The Triangle 填表法,普通dp
    NYOJ-171 聪明的kk 填表法 普通dp
    NYOJ17 最长单调递增子序列 线性dp
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6261996.html
Copyright © 2011-2022 走看看