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, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:"9,3,4,#,#,1,#,#,2,#,6,#,#"
Return true
Example 2:"1,#"
Return false
Example 3:"9,#,#,1"
Return false
1. 栈
class Solution {
public:
bool isValidSerialization(string preorder) {
stack<char> stk;
bool isNum = false;
preorder.push_back(','); // dummy tail
for(auto c: preorder){
if(c == '#'){
// absorb: search for pattern `#, number` backward
while(!stk.empty() && stk.top() == '#'){
stk.pop(); // pop `#`
if(stk.empty() || stk.top() == '#') return false; // pattern `#,#,#`
stk.pop(); // pop `number`
}
stk.push('#'); // replace `number` with `#` since it has been fully explored/validated
}else if(c == ','){
if(isNum) stk.push('n'); // indicate this is a number instead of using the real number
isNum = false;
}else{
isNum = true;
}
}
return stk.size() == && stk.top() == '#';
}
};
2. 不用栈
class Solution {
public:
bool isValidSerialization(string preorder) {
string& s = preorder;
while (s.size() >= ) {
bool find_pattern = false;
for (int i = s.size()-; i>= ; i--) {
if (s[i] == '#' && s[i-] == '#' && s[i-] != '#') {
find_pattern = true;
int j = i--;
/* find the start place of pattern */
while (j > && s[j] != ',') j--;
s.replace(j+, i-j, "#"); /* replace s[j+1, i] to "#" */
break; /* start a trun search from the end */
}
}
if (!find_pattern) break;
}
/* boundary: empty tree */
return (s.size() == && s[] == '#');
}
};
从右向左,若出现(数字,#,#)模式,则替换成一个#。最后若只剩一个#则合法,否则不合法。
331. Verify Preorder Serialization of a Binary Tree -- 判断是否为合法的先序序列的更多相关文章
- leetcode 331. Verify Preorder Serialization of a Binary Tree
传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 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, ...
- 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, ...
- 【leetcode】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 ...
- 331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化
序列化二叉树的一种方法是使用前序遍历.当我们遇到一个非空节点时,我们可以记录这个节点的值.如果它是一个空节点,我们可以使用一个标记值,例如 #. _9_ / \ 3 2 ...
- LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27
331. 验证二叉树的前序序列化 331. Verify Preorder Serialization of a Binary Tree 题目描述 每日一算法2019/5/30Day 27LeetCo ...
- 【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 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
随机推荐
- Django 的 Form组件
Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 Form类的使用: 1.定义规则: from ...
- MySQL 的mysqldump备份
MySQL 的mysqldump备份 来自<mysql技术内幕 innodb存储引擎> --single-transaction:只对innodb表有效 --lock-tables:对My ...
- Shader工具
1. RenderMonkey 2. NVIDIA FX Composer 2.5
- [py][mx]django实现根据城市和课程机构类别过滤
实现根据城市&课程机构过滤 实现点谁谁高亮,支持取交集. 直接上代码吧 本质上是过滤,多层过滤,取交集 def get(self, request): all_orgs = CourseOrg ...
- nmon 命令
nmon 命令 用途 以交互方式显示本地系统统计信息并以记录方式记录系统统计信息. 语法 交互方式: nmon [ -h ] nmon [ -s < seconds >] [ -c < ...
- Linux系统——防火墙
防火墙的作用 一种将内部网络和外部网络分开的方法,是一种隔离技术.防火墙在内网与外网通信是进行访问控制,一句锁设置的规则对数据包做出判断,最大限度地阻止网络中的黑客破坏企业网络,从而加强企业网络安全. ...
- 基于Policy Gradient实现CartPole
http://chenrudan.github.io/blog/2016/09/04/cartpole.html 首页 分类 关于 归档 标签 基于Policy Gradient实现CartPole ...
- 五,动态库(dll)的封装与使用
在项目开发中,我们经常会使用到动态库(dll),要么是使用别人的动态库,要么是将功能函数封装为动态库给别人用.那么如何封装和使用动态库呢?以下内容为你讲解. 1.动态库的封装 以vs2010为例,我们 ...
- Python3:sqlalchemy对mysql数据库操作,非sql语句
Python3:sqlalchemy对mysql数据库操作,非sql语句 # python3 # author lizm # datetime 2018-02-01 10:00:00 # -*- co ...
- # 20145106 《Java程序设计》第3周学习总结
教材学习内容总结 在本周的学习中,我看到了这样一句话:"使用java撰写程序几乎都是在使用对象(object),要产生对象必须先定义类(class),类是对象的设计图,对象是类的实例(ins ...