检验二叉树序列化的合理性 Verify Preorder Serialization of a Binary Tree
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的更多相关文章
- LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27
331. 验证二叉树的前序序列化 331. Verify Preorder Serialization of a Binary Tree 题目描述 每日一算法2019/5/30Day 27LeetCo ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- leetcode 331. Verify Preorder Serialization of a Binary Tree
传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...
- [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, ...
- [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, ...
- 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, ...
- 【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 ...
- LeetCode Verify Preorder Serialization of a Binary Tree
原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...
- 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, ...
随机推荐
- thinkphp 隐藏表单验证原理
function savetoken() //创建session('hash') ,然后在魔板中表单中加入隐藏域 getsession('hash'),提交表单验证值是否一样,如果一样验证通过,同时重 ...
- Impala和Hive的关系(详解)
Impala和Hive的关系 Impala是基于Hive的大数据实时分析查询引擎,直接使用Hive的元数据库Metadata,意味着impala元数据都存储在Hive的metastore中.并且im ...
- python练习题-写一个函数,打印所有包含copy方法的内置对象
代码: #encoding=utf-8for i in dir(__builtins__): #print "i:",i try: #这里的i是个字符串,并不能直接用d ...
- javashop组件开发指南
javashop组件开发指南 1. 概念解释 组件:可以理解为是插件,功能点的一个集合. 插件:是指具体的某个功能. 插件桩:是负责调用插件. 事件:是要决定什么时候执行插件 一个组件是由多 ...
- 远程获得的有趣的linux命令
使用这些工具从远程了解天气.阅读资料等. 我们即将结束为期 24 天的 Linux 命令行玩具日历.希望你有一直在看,如果没有,请回到开始,从头看过来.你会发现 Linux 终端有很多游戏.消遣和奇怪 ...
- android 简单的控件前端代码
/Hello_word/res/layout/activity_main.xml Graphical Layout/activity_fullsreen.xml(layout/) 代码与设置界面互换 ...
- python进程编程
多进程multiprocess模块 multiprocessing is a package that supports spawning processes using an API similar ...
- 算法之路 level 01 problem set
2992.357000 1000 A+B Problem1214.840000 1002 487-32791070.603000 1004 Financial Management880.192000 ...
- 02: SocketServer服务
网络编程其他篇 目录: 1.1 SocketServer四种基本流及 异步处理理论部分 1.2 创建socketserver实现: 多客户端并发 1.3 SocketServer实现多并发FTP 部分 ...
- 20145307陈俊达《网络对抗》Exp7 网络欺诈技术防范
20145307陈俊达<网络对抗>Exp7 网络欺诈技术防范 基础问题回答 什么是dns欺骗攻击! 利用dns spoof运行DNS欺骗,如果是请求解析某个域名,dnsspoof会让该域名 ...