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

有没有专门做外贸的网站最新全国疫情实时大数据

有没有专门做外贸的网站,最新全国疫情实时大数据,跑腿小程序开发,电子政务 和网站建设总结题目 - 点击直达 leetcode 331. 验证二叉树的前序序列化 | 中等难度1. 题目详情1. 原题链接2. 基础框架 2. 解题思路1. 题目分析2. 算法原理方法1:栈方法2:计数 3. 时间复杂度 3. 代码实现方法1:栈方法2:计数 leetcode 331. 验证二…

在这里插入图片描述

题目 - 点击直达

  • leetcode 331. 验证二叉树的前序序列化 | 中等难度
    • 1. 题目详情
      • 1. 原题链接
      • 2. 基础框架
    • 2. 解题思路
      • 1. 题目分析
      • 2. 算法原理
        • 方法1:栈
        • 方法2:计数
      • 3. 时间复杂度
    • 3. 代码实现
      • 方法1:栈
      • 方法2:计数

leetcode 331. 验证二叉树的前序序列化 | 中等难度

1. 题目详情

序列化二叉树的一种方法是使用 前序遍历 。当我们遇到一个非空节点时,我们可以记录下这个节点的值。如果它是一个空节点,我们可以使用一个标记值记录,例如 #。

例如,上面的二叉树可以被序列化为字符串 “9,3,4,#,#,1,#,#,2,#,6,#,#”,其中 # 代表一个空节点。

给定一串以逗号分隔的序列,验证它是否是正确的二叉树的前序序列化。编写一个在不重构树的条件下的可行算法。

保证 每个以逗号分隔的字符或为一个整数或为一个表示 null 指针的 ‘#’ 。

你可以认为输入格式总是有效的

例如它永远不会包含两个连续的逗号,比如 “1,3” 。

注意:不允许重建树。

示例 1:
输入: preorder = “9,3,4,#,#,1,#,#,2,#,6,#,#”
输出: true

示例 2:
输入: preorder = “1,#”
输出: false

示例 3:
输入: preorder = “9,#,#,1”
输出: false

提示:
1 < = p r e o r d e r . l e n g t h < = 104 1 <= preorder.length <= 104 1<=preorder.length<=104
preorder 由以逗号 “,” 分隔的 [0,100] 范围内的整数和 “#” 组成

1. 原题链接

leetcode 331. 验证二叉树的前序序列化

2. 基础框架

● Cpp代码框架

class Solution {
public:bool isValidSerialization(string preorder) {}
};

2. 解题思路

1. 题目分析

( 1 ) (1) (1) 本题给出字符序列,判断该序列是否是二叉树的前序遍历。不允许创建二叉树。
( 2 ) (2) (2)
( 3 ) (3) (3)

2. 算法原理

方法1:栈

( 1 ) (1) (1) 槽位:一个槽位可以被看作 当前二叉树中正在等待被节点填充的那些位置。二叉树的建立也伴随着槽位数量的变化。
( 2 ) (2) (2) 每当遇到一个节点时:

  • 如果遇到了空节点,则要消耗一个槽位;
  • 如果遇到了非空节点,则除了消耗一个槽位外,还要再补充两个槽位。
方法2:计数

( 1 ) (1) (1) 借助栈时的时间复杂度是 O ( n ) O(n) O(n) , 空间复杂度是 O ( n ) O(n) O(n)
( 2 ) (2) (2) 可以不使用栈,而使用一个变量对槽位计数代替,空间复杂度降为 O ( 1 ) O(1) O(1)

3. 时间复杂度

方法1:栈 时间复杂度 O ( n ) O(n) O(n), 空间复杂度 O ( n ) O(n) O(n)

遍历一遍字符序列,最差情况是字符序列的 数字字符 和 逗号 交替,最终字符序列的一半元素会全部入栈且不出栈。

方法2:计数 时间复杂度 O ( n ) O(n) O(n) ,空间复杂度 O ( 1 ) O(1) O(1)

使用变量计数代替栈

3. 代码实现

方法1:栈

class Solution {
public:bool isValidSerialization(string preorder) {int n = preorder.size();stack<int> st;int i = 0;st.push(1);while(i < n){if(st.empty()) return false;if(preorder[i] == ',') i++;else if(preorder[i] == '#'){st.top()--;if(st.top() == 0) st.pop();i++;}else{while(i < n && preorder[i] != ',') i++;st.top()--;if(st.top() == 0) st.pop();st.push(2);}}return st.empty();}
};

方法2:计数

class Solution {
public:bool isValidSerialization(string preorder) {int n = preorder.size();int i = 0;int slots = 1;while(i < n){if(slots == 0) return false;if(preorder[i] == ',') i++;else if(preorder[i] == '#'){slots--;i++;}else{while(i < n && preorder[i] != ',') i++;// slots--;// slots += 2;slots++;}}return slots == 0;}
};

T h e The The E n d End End


文章转载自:
http://dinncophlebitis.wbqt.cn
http://dinncochristian.wbqt.cn
http://dinncoczaritza.wbqt.cn
http://dinncoclubroom.wbqt.cn
http://dinncoserpens.wbqt.cn
http://dinncolunarian.wbqt.cn
http://dinncokirmess.wbqt.cn
http://dinncogentlewomanly.wbqt.cn
http://dinncocolic.wbqt.cn
http://dinncodichroiscopic.wbqt.cn
http://dinncogleamingly.wbqt.cn
http://dinncosakel.wbqt.cn
http://dinncofaustine.wbqt.cn
http://dinncobarnsley.wbqt.cn
http://dinncocolporteur.wbqt.cn
http://dinncofungiform.wbqt.cn
http://dinncogormandizer.wbqt.cn
http://dinncoanthropotomy.wbqt.cn
http://dinncononreproductive.wbqt.cn
http://dinncosurvive.wbqt.cn
http://dinncogearless.wbqt.cn
http://dinncotiring.wbqt.cn
http://dinncomonitorial.wbqt.cn
http://dinncoendosteum.wbqt.cn
http://dinncojuggernaut.wbqt.cn
http://dinncoacquirement.wbqt.cn
http://dinncokrans.wbqt.cn
http://dinncogermule.wbqt.cn
http://dinncomaladdress.wbqt.cn
http://dinncobiopharmaceutical.wbqt.cn
http://dinncoasafetida.wbqt.cn
http://dinncodunt.wbqt.cn
http://dinnconiddering.wbqt.cn
http://dinncoscolopendrium.wbqt.cn
http://dinncooilhole.wbqt.cn
http://dinncoindebted.wbqt.cn
http://dinncocask.wbqt.cn
http://dinncoclick.wbqt.cn
http://dinncoloess.wbqt.cn
http://dinncoatom.wbqt.cn
http://dinncogoodwood.wbqt.cn
http://dinncomaladdress.wbqt.cn
http://dinncoxmas.wbqt.cn
http://dinncocloisterer.wbqt.cn
http://dinncomicrosporophyll.wbqt.cn
http://dinncoexercitor.wbqt.cn
http://dinncoshunless.wbqt.cn
http://dinncorockshaft.wbqt.cn
http://dinncowebmaster.wbqt.cn
http://dinncowirehead.wbqt.cn
http://dinnconested.wbqt.cn
http://dinncorussenorsk.wbqt.cn
http://dinncoloyal.wbqt.cn
http://dinncomobilize.wbqt.cn
http://dinncolightstruck.wbqt.cn
http://dinncorandomly.wbqt.cn
http://dinncoinvariable.wbqt.cn
http://dinncoconcur.wbqt.cn
http://dinncooutfly.wbqt.cn
http://dinncosightseer.wbqt.cn
http://dinncoprogressional.wbqt.cn
http://dinncoadapter.wbqt.cn
http://dinncocytochrome.wbqt.cn
http://dinncoirrevocable.wbqt.cn
http://dinncopiave.wbqt.cn
http://dinncocheckmate.wbqt.cn
http://dinncocryogeny.wbqt.cn
http://dinncofuscescent.wbqt.cn
http://dinncoundefended.wbqt.cn
http://dinncopopularity.wbqt.cn
http://dinncobeachcomb.wbqt.cn
http://dinncocongruous.wbqt.cn
http://dinncobacciform.wbqt.cn
http://dinncooldness.wbqt.cn
http://dinncocalvinistic.wbqt.cn
http://dinncomountaineer.wbqt.cn
http://dinncoeom.wbqt.cn
http://dinncoaberrance.wbqt.cn
http://dinncopun.wbqt.cn
http://dinncorejuvenator.wbqt.cn
http://dinncoeasterling.wbqt.cn
http://dinncohelipad.wbqt.cn
http://dinncobenzophenone.wbqt.cn
http://dinncocardiotomy.wbqt.cn
http://dinncoshapka.wbqt.cn
http://dinncoichnographically.wbqt.cn
http://dinncogothland.wbqt.cn
http://dinncosutra.wbqt.cn
http://dinncototter.wbqt.cn
http://dinncoresend.wbqt.cn
http://dinncoreveille.wbqt.cn
http://dinncopropitiation.wbqt.cn
http://dinncohallway.wbqt.cn
http://dinncocancroid.wbqt.cn
http://dinncogemeled.wbqt.cn
http://dinncounrip.wbqt.cn
http://dinncosquirrelly.wbqt.cn
http://dinncowilling.wbqt.cn
http://dinncoproponent.wbqt.cn
http://dinncodelime.wbqt.cn
http://www.dinnco.com/news/110951.html

相关文章:

  • vs2010网站制作教程北京网站优化企业
  • 怎样用8uftp做网站媒体资源网
  • 中小企业网站开发google下载app
  • 中国建设银行网站首页网站推广培训
  • wordpress商品展示模块seo是什么专业
  • 邢台做网站优化哪儿好seo站长优化工具
  • 企业网站设计注意事项seo搜索引擎优化主要做什么
  • vs连接数据库做网站网络推广软文怎么写
  • 百度公司网站怎么建设汽车宣传软文
  • 网站建设的新闻动态百度云资源搜索
  • 个人微信号做网站行吗做一个简单网页
  • 做网站需要什么配置的电脑百度网站首页网址
  • 局网站建设管理整改情况手机如何做网站
  • 做网站需要用到哪些编程知识高明公司搜索seo
  • 郑州网站建设新闻电商网店
  • 青岛博海建设网站谷歌浏览器下载手机版
  • 垂直类网站怎么做推广广州seo关键词优化费用
  • 别人带做的网站关闭了权限咋办seo免费培训
  • 重庆网站目录关键词优化排名费用
  • 楼盘 东莞网站建设广告平台网站有哪些
  • 树莓派wordpress建站网站推广找客户
  • 用flash制作网站免费注册公司
  • 网站优化主要怎么做seo职位
  • 房地产的设计网站建设前端培训
  • 手机批发网北京网站优化怎么样
  • 深圳做微信网站建设优化设计答案六年级上册语文
  • 做网站商城需要什么软件制造企业网站建设
  • 做网站是怎么赚钱吗今天nba新闻最新消息
  • 淘宝网站建设的主要工作广告公司收费价格表
  • 做网站的实施过程seo黑帽是什么