2018-07-31 17:47:13

问题描述:

问题求解:

本题要求在不构建二叉树的情况下对先序遍历生成的序列化字符串进行合法性验证,这里有个技巧性较强的验证方法,就是采用当前可用的指针数目进行验证,最初的时候只有一个指针,每当遇到一个节点,那么需要消耗一个指针,同时,如果是非空节点需要额外增加两个指针。在遍历过程中一旦出现了指针数目为负数的情况,那么就可以直接判false,最后如果指针数目不为0的话,也直接判false。(这里默认如果序列化为空字符串,则直接判false)

    public boolean isValidSerialization(String preorder) {
String[] strs = preorder.split(",");
int arrow = 1;
for (String str : strs) {
if (--arrow < 0) return false;
if (!str.equals("#")) arrow += 2;
}
return arrow == 0;
}

检验二叉树序列化的合理性 Verify Preorder Serialization of a Binary Tree的更多相关文章

  1. LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27

    331. 验证二叉树的前序序列化 331. Verify Preorder Serialization of a Binary Tree 题目描述 每日一算法2019/5/30Day 27LeetCo ...

  2. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  3. leetcode 331. Verify Preorder Serialization of a Binary Tree

    传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...

  4. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  5. [Swift]LeetCode331. 验证二叉树的前序序列化 | Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  6. Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  7. 【LeetCode】Verify Preorder Serialization of a Binary Tree(331)

    1. Description One way to serialize a binary tree is to use pre-order traversal. When we encounter a ...

  8. LeetCode Verify Preorder Serialization of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...

  9. LeetCode OJ 331. Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

随机推荐

  1. CFA

    拜耳色彩滤波阵列(Bayer Color Filter Array, CFA)是非常有名的彩色图片的数字采集格式.由1/2的G,1/4得R,1/4的B组成. 当Image Sensor向外逐行输出数据 ...

  2. MAX_STATEMENT_TIME uses confusing syntax

    From   https://bugs.mysql.com/bug.php?id=72540   [5 May 2014 18:46] Morgan Tocker Description: Via C ...

  3. UVALive - 7269 I - Snake Carpet

    思路: 多画画就发现从五的时候可以这么填: 六的时候这么填: 七的时候这么填: 看出规律了吗? 没看出的话再画画把. #include <bits/stdc++.h> using name ...

  4. zoj3814

    这题说的是给了一个数在longlong范围内 然后求出小于这个数的最大的回文,枚举每位减去1后 , 他后面的位置上全部都置为9,然后在枚举每个前半部分,然后贪心取得这个数的最大值,贪心的时候写错了,错 ...

  5. 隐马尔可夫模型(HMM)学习笔记一

    学习了李航的<统计学习方法>中隐马尔可夫模型(Hidden Markov Model, HMM),这里把自己对HMM的理解进行总结(大部分是书本原文,O(∩_∩)O哈哈~,主要是想利用py ...

  6. Python: 没有switch-case语句

    初学Python语言,竟然很久才发现Python没有switch-case语句 官方的解释说,“用if... elif... elif... else序列很容易来实现 switch / case 语句 ...

  7. Storm消息可靠处理机制

    在很多应用场景中,分布式系统的可靠性保障尤其重要.比如电商平台中,客户的购买请求需要可靠处理,不能因为节点故障等原因丢失请求:比如告警系统中,产生的核心告警必须及时完整的知会监控人员,不能因为网络故障 ...

  8. linux常用命令:tail 命令

    tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新, ...

  9. EasyUI+bootsrtap混合前端框架

    EasyUI+bootsrtap混合前端框架 http://www.jeasyui.com/download/index.php用户没有登录前浏览的页面用bootsrtap框架用户登录进去后的商家管理 ...

  10. sql server 数据排名

    城市排名列表 )) AS px, ) pm25, ) pm10, ) co, ) no2, ) so2,) o3_8,) indexs,) aqi FROM monitor_city_hour m,c ...