zoukankan      html  css  js  c++  java
  • 2021/8/29

    CCPC网络赛

    一场笔试

    刷三题

    总结复盘最近所学的知识。

     看了八章和第九章的一部分。

    一题leetcode

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Codec {
    public:
    
        // Encodes a tree to a single string.
        string serialize(TreeNode* root) {
            string ans = "";
            if(root == NULL) return ans;
            queue<TreeNode*> Q;
            Q.push(root);
            while(!Q.empty()){
                TreeNode *T = Q.front(); Q.pop();
                if(T != NULL){
                    ans += to_string(T -> val);
                    ans += "*"; 
                    Q.push(T -> left); Q.push(T -> right);
                }
                else ans += "NULL*";
            }
            cout << "str " << ans << endl;
            return ans;
        }
    
        // Decodes your encoded data to tree.
        void dfs(TreeNode * &root,string &data,int &l){
            queue<TreeNode**>Q;
            Q.push(&root);
            while(!Q.empty()){
                TreeNode **T = Q.front(); Q.pop();
                int len = 0;
                if(data[l] == 'N'){
                    *T = NULL; l += 5;
                } else {
                    int s = l;
                    while(l < data.size() && ((data[l] >= '0' && data[l] <= '9') || data[l] == '-')){
                        l ++; len ++;
                    }
                    string str = data.substr(s,len);
                    int n = atoi(str.c_str());
                    while(l < data.size() && data[l] == '*') ++ l;
                    *T = (TreeNode *)malloc(sizeof(TreeNode));
                    (*T) -> val = n;
                    (*T) -> left = (*T) -> right = NULL;
    
                    Q.push(&((*T) -> left));
                    Q.push(&((*T) -> right));
    
                }
            }
            
           
            
            return ;
        }
    
        TreeNode* deserialize(string data) {
            TreeNode *root = NULL;
            if(data.size() == 0) return root;
            int l = 0;
            dfs(root,data,l);
            return  root;
        }
    };
    
    // Your Codec object will be instantiated and called as such:
    // Codec ser, deser;
    // TreeNode* ans = deser.deserialize(ser.serialize(root));
    

      

  • 相关阅读:
    python simplejson and json 使用及区别
    python 网页抓取并保存图片
    word2vec剖析,资料整理备存
    centos 安装LAMP环境后装phpmyadmin
    centos 安装卸载软件命令 & yum安装LAMP环境
    Ubuntu终端快捷键使用
    Ubuntu终端文件的压缩和解压缩命令
    文本预处理去除标点符号
    朴素贝叶斯分类器的应用
    Win32环境下代码注入与API钩子的实现(转)
  • 原文地址:https://www.cnblogs.com/yrz001030/p/15201298.html
Copyright © 2011-2022 走看看