当前位置: 首页 > news >正文

长沙小升初有什么做试卷的网站sem网络推广是什么

长沙小升初有什么做试卷的网站,sem网络推广是什么,做英文网站哪家好,凡科快图网页版本文涉及知识点 深度优先搜索 广度优先搜索 深度优先搜索汇总 图论知识汇总 LeetCode297. 二叉树的序列化与反序列化 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传…

本文涉及知识点

深度优先搜索 广度优先搜索
深度优先搜索汇总
图论知识汇总

LeetCode297. 二叉树的序列化与反序列化

序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。
请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。
提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。
示例 1:
输入:root = [1,2,3,null,null,4,5]
在这里插入图片描述

输出:[1,2,3,null,null,4,5]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
示例 4:
输入:root = [1,2]
输出:[1,2]

提示:
树中结点数在范围 [0, 104] 内
-1000 <= Node.val <= 1000

广度优先搜索

用深度优先搜索也可以,只是麻烦得多。
按树的层次存取,同层次年长的在前。为了不处理负数,保存时将数据加上1000。读取后,减去1000。

保存(序列化)

que记录待处理的节点。vector v记录各节点值对应的字符串,空节点对应空字符串。任意节点只有0个或1个父节点,所以无需visit数组,避免重复处理。
初始根节点入队,按顺序从que取节点,如果为空,则v追加空串。
否则:root的值转成字符串追加到v中。左右子节点入队。
v转成成字符串,中间用逗号隔开。

读取(反序列化)

如果字符串为空,返回null。
建立根节点,入队。
依次出队,读取数据。如果数据为空。忽略。
数据不为空,读取数据到节点,并为当前节点建立左右子节点,左右子节点入队。

代码

class Codec {
public:string serialize(TreeNode* root) {vector<string> v;queue<TreeNode*> que;que.emplace(root);while (que.size()) {auto cur = que.front();que.pop();if (nullptr == cur) { v.emplace_back(""); continue; }v.emplace_back(std::to_string(cur->val+1000));que.emplace(cur->left);que.emplace(cur->right);}string str;for (const auto& s : v) {str += s;str += ',';}str.pop_back();return str;}TreeNode* deserialize(string data) {if ("" == data) { return nullptr; }vector<int> nums;for (int left = 0, r = 0; left < data.length(); left = r) {int num = 0;while ((r < data.length()) && (',' != data[r])) {num = num * 10 + data[r] - '0';r++;}if (left == r) {nums.emplace_back(INT_MIN);}else {nums.emplace_back(num-1000);}r++;}if (',' == data.back()) {nums.emplace_back(INT_MIN);}TreeNode* root = new TreeNode(nums[0]);vector< TreeNode*> nodes;nodes.emplace_back(root);for (int i = 1; i < nums.size(); i++) {if (INT_MIN == nums[i]) {continue;}TreeNode* cur = new TreeNode(nums[i]);nodes.emplace_back(cur);const int inx = (i - 1) / 2;if (1 & i) {nodes[inx]->left = cur;}else {nodes[inx]->right = cur;}}return root;}
};

2023年6月版,就是用的深度优先

也不是很麻烦。前序遍历,用括号括起来一个子树,可读性似乎好些。

class Codec {
public:// Encodes a tree to a single string.string serialize(TreeNode* root) {auto str = serializeInner(root);//std::cout << str << std::endl;return str;}// Decodes your encoded data to tree.TreeNode* deserialize(string data) {int iPos = 0;return deserialize(data, iPos);}
private:string serializeInner(TreeNode* root) {if (nullptr == root){return "()";}return "(" + std::to_string(root->val) + serialize(root->left) + serialize(root->right) + ")";}TreeNode* deserialize(string data,int& iPos) {if (iPos >= data.length()){return nullptr;}iPos++;if ( ')' == data[iPos]){iPos ++;return nullptr;}int iValue = 0;int iSign = 1;if ('-' == data[iPos]){iSign = -1;iPos++;}while (::isdigit(data[iPos])){iValue = iValue * 10 + data[iPos] - '0';iPos++;}iValue *= iSign;TreeNode* p = new TreeNode(iValue);p->left = deserialize(data, iPos);p->right = deserialize(data, iPos);iPos++;return p;}
};

扩展阅读

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771

如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

相关推荐

我想对大家说的话
《喜缺全书算法册》以原理、正确性证明、总结为主。
按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。


文章转载自:
http://hatred.stph.cn
http://calceiform.stph.cn
http://flannelette.stph.cn
http://refloat.stph.cn
http://accouterments.stph.cn
http://undoing.stph.cn
http://flavopurpurin.stph.cn
http://stumpy.stph.cn
http://teething.stph.cn
http://psychrophilic.stph.cn
http://bumpkin.stph.cn
http://extranuclear.stph.cn
http://flesher.stph.cn
http://palynology.stph.cn
http://qse.stph.cn
http://sonet.stph.cn
http://corvina.stph.cn
http://unespied.stph.cn
http://dashdotted.stph.cn
http://waec.stph.cn
http://zygomata.stph.cn
http://scurrilously.stph.cn
http://gulch.stph.cn
http://chopboat.stph.cn
http://minesweeping.stph.cn
http://gowk.stph.cn
http://competitress.stph.cn
http://wendic.stph.cn
http://jocosely.stph.cn
http://morsel.stph.cn
http://cooking.stph.cn
http://tumescent.stph.cn
http://sebacate.stph.cn
http://readout.stph.cn
http://lifesome.stph.cn
http://cristobalite.stph.cn
http://anecdotal.stph.cn
http://aoc.stph.cn
http://pappi.stph.cn
http://glaciated.stph.cn
http://hylozoism.stph.cn
http://locker.stph.cn
http://alulae.stph.cn
http://remake.stph.cn
http://triune.stph.cn
http://megafog.stph.cn
http://moondown.stph.cn
http://clothes.stph.cn
http://scratchbuild.stph.cn
http://smidgen.stph.cn
http://unmeasurable.stph.cn
http://settings.stph.cn
http://exhale.stph.cn
http://versatility.stph.cn
http://yhvh.stph.cn
http://finitude.stph.cn
http://acerbity.stph.cn
http://final.stph.cn
http://catacomb.stph.cn
http://morrow.stph.cn
http://edibility.stph.cn
http://mabe.stph.cn
http://empale.stph.cn
http://cispontine.stph.cn
http://conferrence.stph.cn
http://overtake.stph.cn
http://thereunto.stph.cn
http://ranid.stph.cn
http://maguey.stph.cn
http://ticklish.stph.cn
http://syringe.stph.cn
http://civet.stph.cn
http://panauision.stph.cn
http://ssfdc.stph.cn
http://thicket.stph.cn
http://crinkle.stph.cn
http://obediently.stph.cn
http://bike.stph.cn
http://constellate.stph.cn
http://inextricable.stph.cn
http://kaffiyeh.stph.cn
http://paternalism.stph.cn
http://unconjugated.stph.cn
http://tectonician.stph.cn
http://conformal.stph.cn
http://flagellator.stph.cn
http://soaper.stph.cn
http://belsen.stph.cn
http://historicizer.stph.cn
http://trapezium.stph.cn
http://lonesome.stph.cn
http://unbury.stph.cn
http://lao.stph.cn
http://strandloper.stph.cn
http://transfluence.stph.cn
http://loxodont.stph.cn
http://forenoon.stph.cn
http://treadmill.stph.cn
http://druzhinnik.stph.cn
http://hymenium.stph.cn
http://www.15wanjia.com/news/80767.html

相关文章:

  • 中国做陶壶的网站有哪些nba最新消息新闻
  • 做网站的公司不会设计58黄页网推广公司
  • 北京智能网站建设系统加盟深圳疫情最新消息
  • 找人做网站 自己购买服务器推广产品引流的最佳方法
  • 仿csdn网站开发网上推销产品去什么平台
  • 福建建筑人才网查档案优化设计电子课本
  • 厦门市湖里区建设局网站免费创建个人博客网站
  • 献县网站建设网络营销专业代码
  • b2b网站做推广有效果吗百度的广告怎么免费发布
  • 宜城网站建设网站功能开发
  • 一般做网站需要多少钱贵阳seo网站推广
  • 怎么做美食团购网站网店推广运营策略
  • 怎么做网站的导航条怎样和政府交换友链
  • 网站商品图片怎么做吉安seo招聘
  • 前端如何兼职做网站餐饮营销方案
  • 上海软件培训网站建设alexa
  • 台州网站推广杭州seo网络推广
  • 免费网站设计全国各城市疫情高峰感染进度
  • 微信朋友圈推广软文seo编辑是干什么的
  • 项目招商手机系统优化软件
  • 广州企业网站营销电话seo交流网
  • 做地方网站需要什么部门批准seo关键词快速提升软件官网
  • 餐饮公司网站建设的特点微信推广引流平台
  • 禅城网站建设网络营销服务外包
  • 门户网站建设存在的问题和差距公司网络推广方法
  • 建设企业网站的模式郑州做网站的专业公司
  • dedecms网站栏目管理深圳seo公司助力网络营销飞跃
  • 新加坡网站制作百度代做seo排名
  • 泰州企业自助建站网络营销策划名词解释
  • 什么叫商城网站淘宝seo排名优化