检验二叉树序列化的合理性 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, ...
随机推荐
- centos添加epel源
1. rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm 粗体部分需要根据自己的 ...
- http-equiv="Refresh" 实现定时刷新页面
***.html自动跳转文件代码如下: <HTML> <HEAD><META http-equiv="Refresh" content="5 ...
- cygwin本地.bashrc配置
echo -e "====================================================================================== ...
- Linux服务器---配置apache支持用户认证
Apache支持用户认证 为了服务器的安全,通常用户在请求访问某个文件夹的时候,Apache可以要求用户输入有效的用户名和登录密码 1.创建一个测试目录 [root@localhost cgi-bin ...
- 访问Hsql .data数据库文件
一.Hsql简介: hsql数据库是一款纯Java编写的免费数据库,许可是BSD-style的协议. 仅一个hsqldb.jar文件就包括了数据库引擎,数据库驱动,还有其他用户界面操作等内容.下载地址 ...
- [分享] 采用opencv_cascadetrain进行训练的步骤及注意事项 [复制链接]
http://f.dataguru.cn/thread-725364-1-1.html 很有用的一个帖子 转自:http://blog.csdn.net/xidianzhimeng/article/d ...
- Centos6版本使用yum报错 Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfi Setting up Install Process No package gcc available. Error: Nothing to do
在使用Centos6版本yum时报错 Loaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds ...
- Mysql优化_内置profiling性能分析工具
如果要进行SQL的调优优化和排查,第一步是先让故障重现,但是这个并不是这一分钟有问题,下一秒就OK.一般的企业一般是DBA数据库工程师从监控里找到问题.DBA会告诉我们让我们来排查问题,那么可能很多种 ...
- 07: linux中正则表达式与grep使用
1.1 linux中正则表达式 1.^linux 以linux开头的行 2.$php 以php结尾的行 3.. 匹配任意单字符 4..+ ...
- zabbix zabbix_agentd.conf详解
# This is a config file for the Zabbix agent daemon (Unix) # To get more information about Zabbix, v ...