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, ...
随机推荐
- Java编程思想第四版勘误
坊间传说这本书翻译得很烂,我倒觉得还好.虽然看原文更准确,但是如果在具备一定编程思维和基础.能够看出来疑问的情况下,还是看中文更快一些,而且这本书本身也不适合初学者看.当然,错误和不通顺还是有的,而且 ...
- python 对shell 命令的 执行 逻辑 在一台机器上执行另一台机器的命令; 跨节点 执行命令
import os l = ['ssh a;scp /data/visitlog/*11* root@d:/data/mapReduceVisitorLog/'] # b c for i in l: ...
- Qt 模拟鼠标点击(QApplication::sendEvent(ui->pushbutton, &event0);)
QPoint pos(0,0);QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt ...
- CSS之Flex 布局:语法篇
网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如 ...
- 003-and design-在create-react-app项目中使用antd
一.概述 create-react-app 是业界最优秀的 React 应用开发工具之一,本文会尝试在 create-react-app 创建的工程中使用 antd 组件,并自定义 webpack 的 ...
- go-003-基础语法
1.行分隔符 一行代表一个语句结束. 如果一行多个,使用“;”分割,不推荐使用,建议使用默认一行一个语句 2.标识符 标识符用来命名变量.类型等程序实体.一个标识符实际上就是一个或是多个字母(A~Z和 ...
- 数据库知识,mysql索引原理
1:innodb底层实现原理:https://blog.csdn.net/u012978884/article/details/52416997 2:MySQL索引背后的数据结构及算法原理 ht ...
- 一个简单的3D范例,是在别人基础上面整理的。
一个简单的范例,是在别人基础上面整理的.原来的例子,框图太乱了,没有条理感. http://pan.baidu.com/s/1eQTyGCE
- 466E - Information Graph 巧妙的判断祖先于孩子的关系
这题说的是给了一个公司员工100000 然后现在又3种操作第一种将y置为x的父亲,第二种操作将文件给第x个人签他签完给他的上司签,一直到没有上司为止,第三种操作问x是否签了第i份文件,然后 我们只要直 ...
- Java 强引用、软引用、弱引用、幻象引用有什么区别
1)引用出现的根源 引用出现的根源是由于GC内存回收的基本原理.GC回收本质上是回收对象.目前比较流行的回收算法是可达性分析算法.从GC roots开始安装一定的逻辑判断一个对象是否可达,不可达的话就 ...