博客
关于我
剑指offer Leetcode 37.序列化二叉树
阅读量:313 次
发布时间:2019-03-03

本文共 3610 字,大约阅读时间需要 12 分钟。

image-20201209153920882

注意:序列化和反序列化只要能可逆就行,后面可以多出几个NULL

注意:string用+= 和push_back()效率要高于 用+,用+需要创建一个新的stirng对象

序列化:层序遍历

思想:

​ 逐层写入

复杂度:

​ ●时间:遍历每一个节点:O(n)

​ ●空间:O(n),利用队列

代码:

// Encodes a tree to a single string.    string serialize(TreeNode* root) {           //询问面试官为空时是string是空还是有个NULL,为空则要加判root是否为空,为NULL则不用        queue
my_queue; my_queue.push(root); string str_tree; //层序遍历 while(!my_queue.empty()){ TreeNode* cur = my_queue.front(); my_queue.pop(); //不为空则写入 if(cur != NULL){ str_tree += to_string(cur->val); str_tree += ","; my_queue.push(cur->left); my_queue.push(cur->right); } else str_tree += "NULL,"; } //去掉最后的逗号 str_tree.pop_back(); return str_tree; }

反序列化

思想:

​ 将string中的每个节点分隔开并放入vector<TreeNode*>,并利用vector<TreeNode*>重建

复杂度:

​ 这里获取单个字符串较为复杂,可以使用流的方式。

代码:

TreeNode* deserialize(string data) {           if(data.size() == 0)            return NULL;        int size_data = data.size();         //i用来遍历data        int i = 0;        vector
vec_tree; //将string转为vector
来存储 while(i < size_data){ string str_cur = ""; while(i < size_data && data[i] != ','){ str_cur.push_back(data[i]); i++; } if(str_cur == "NULL") vec_tree.push_back(NULL); else{ TreeNode* tmp = new TreeNode(stoi(str_cur)); vec_tree.push_back(tmp); } //跳过逗号 i++; } //画一下就能理解这个流程,i用来遍历父节点,j用来遍历子节点 for(int i = 0, j = 1; j < vec_tree.size(); i++){ if(vec_tree[i] == NULL) continue; //连接左子树 vec_tree[i]->left = vec_tree[j++]; //连接右子树,因为j加了1,所以需要判断 if(j < vec_tree.size()) vec_tree[i]->right = vec_tree[j++]; } return vec_tree[0]; }

利用流序列化和反序列化

注意:

​ 这里是用空格间隔而不是用",",这样更方便获取str_cur,要用逗号序列化可以在反序列化时先把逗号转为空格

代码:

class Codec {   public:    // Encodes a tree to a single string.    string serialize(TreeNode* root) {           ostringstream out;        queue
my_queue; my_queue.push(root); while(!my_queue.empty()){ TreeNode* cur = my_queue.front(); my_queue.pop(); if(cur != NULL){ out<
val<<" "; my_queue.push(cur->left); my_queue.push(cur->right); } else out<<"NULL "; } return out.str(); } // Decodes your encoded data to tree. TreeNode* deserialize(string data) { istringstream input(data); string str_cur; vector
vec_node; //利用流来获取单个字符 while(input >> str_cur){ if(str_cur == "NULL") vec_node.push_back(NULL); else vec_node.push_back(new TreeNode(stoi(str_cur))); } // i每往后移动一位,j移动两位,j始终是当前i的左子下标 // 肯定是j先到达边界,所以这里判断j < vec.size() for (int i = 0, j = 1; j < vec_node.size(); ++i) { //某个NULL的下面一层还有别的节点时(类似于NULL为头节点),如[1,N,2,N,N,3,4]就需要这里的判断 if (vec_node[i] == NULL) continue; // 当前j位置为i的左子树 vec_node[i]->left = vec_node[j++]; // 当前j位置为i的右子树,j改变了,所以需要判断 if (j < vec_node.size()) vec_node[i]->right = vec_node[j++]; } return vec_node[0]; }};

转载地址:http://mndq.baihongyu.com/

你可能感兴趣的文章
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>