• 序列化二叉树


    题目描述

    请实现两个函数,分别用来序列化和反序列化二叉树
     
    解题思路:
    利用层次遍历的思想。序列化时,节点值用“ ”空格隔开,空节点使用“#”代替。
    反序列化时把提取出来的节点放入队列中,每次循环都处理的是该节点的左右儿子节点。
    class Solution {
    public:
    
        char* Serialize(TreeNode *root) {
            string res;
            if(root == NULL) return NULL;
            queue<TreeNode*> que;
            TreeNode * tmpNode = NULL;
            que.push(root);
            res += tostring(root->val) + " ";
            while(!que.empty()){
                tmpNode = que.front();
                que.pop();
                if(tmpNode->left != NULL){
                    que.push(tmpNode->left);
                    res+= tostring(tmpNode->left->val) + " ";
                }else{
                    res += "# ";
                }
                if(tmpNode->right != NULL){
                    que.push(tmpNode->right);
                    res+= tostring(tmpNode->right->val) + " ";
                }else{
                    res += "# ";
                }
            }
            int len = res.size();
            char *ret = new char[len+1];
            strcpy(ret, res.c_str());
            return ret;
        }
        TreeNode* Deserialize(char *str) {
            int i = 0;
            if(str == NULL) return NULL;
            if(str[0] == '') return NULL;
            int num = pickNum(str, i);
            queue<TreeNode*> que;
            TreeNode *ret = new TreeNode(num);
            TreeNode* tmpNode = NULL;
            que.push(ret);
            while(!que.empty()){
                tmpNode = que.front();
                que.pop();
                cout<<"tmpNode->val="<<tmpNode->val<<endl;
                cout<<"str[i]="<<str[i]<<endl;
                if(str[i] >= '0' && str[i] <= '9'){
                    int lv = pickNum(str, i);
                    //cout<<"lv="<<lv<<endl;
                    tmpNode->left = new TreeNode(lv);
                    que.push(tmpNode->left);
                }else{
                    if(str[i] == '#'){
                        i+=2;
                    }
                }
                if(str[i] >= '0' && str[i] <= '9'){
                    int rv = pickNum(str, i);
                    //cout<<"rv="<<rv<<endl;
                    tmpNode->right = new TreeNode(rv);
                    que.push(tmpNode->right);
                }else{
                    if(str[i] == '#'){
                        i+=2;
                    }
                }
            }
            return ret;
        }
       //打印二叉树的层次结构
       vector<vector<int> > Print(TreeNode* pRoot) {
                vector<vector<int> > res;
                if(pRoot == NULL) return res;
                queue<TreeNode *> que1, que2;
                que1.push(pRoot);
                res.push_back(vector<int>(1, pRoot->val));
                TreeNode* tmpNode=NULL;
                while(!que1.empty() || !que2.empty()){
                    vector<int>tmpVct;
                    while(!que1.empty()){
                        tmpNode = que1.front();
                        que1.pop();
                        if(tmpNode->left != NULL){
                            que2.push(tmpNode->left);
                            tmpVct.push_back(tmpNode->left->val);
                        }
                        if(tmpNode->right != NULL){
                            que2.push(tmpNode->right);
                            tmpVct.push_back(tmpNode->right->val);
                        }
                    }
                    if(tmpVct.size() != 0)
                        res.push_back(tmpVct);
                    tmpVct.clear();
                    while(!que2.empty()){
                        tmpNode = que2.front();
                        que2.pop();
                        if(tmpNode->left != NULL){
                            que1.push(tmpNode->left);
                            tmpVct.push_back(tmpNode->left->val);
                        }
                        if(tmpNode->right != NULL){
                            que1.push(tmpNode->right);
                            tmpVct.push_back(tmpNode->right->val);
                        }
                    }
                    if(tmpVct.size() != 0)
                        res.push_back(tmpVct);
                }
                return res;
            }
    private:
        string tostring(int a){
            strstream ss;
            ss<<a;
            string res;
            ss>>res;
            return res;
        }
        int pickNum(char *str, int & i){
            int res = 0;
            while(str[i]>='0' && str[i] <= '9'){
                res = res*10 + str[i]-'0';
                i++;
            }
            if(str[i] == ' ') i++;
            return res;
        }
    };
    

      

  • 相关阅读:
    606. Construct String from Binary Tree
    557. Reverse Words in a String III
    551. Student Attendance Record I
    541. Reverse String II
    521. Longest Uncommon Subsequence I
    520. Detect Capital
    459. Repeated Substring Pattern
    人脸检测源码facedetection
    人脸检测的model类facemodel
    人脸检测解析json的工具类face_test
  • 原文地址:https://www.cnblogs.com/chengsheng/p/10702353.html
Copyright © 2020-2023  润新知