331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化
序列化二叉树的一种方法是使用前序遍历。当我们遇到一个非空节点时,我们可以记录这个节点的值。如果它是一个空节点,我们可以使用一个标记值,例如 #。
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
例如,上面的二叉树可以被序列化为字符串"9,3,4,#,#,1,#,#,2,#,6,#,#",其中#代表一个空节点。
给定一串以逗号分隔的值,验证它是否是正确的二叉树的前序序列化。想出一种在不重构树的情况下的可行算法。
每个逗号分隔的字符串中的值肯定是一个整数或者一个表示null指针的'#'。
你可以假定输入格式总是有效的,例如它永远不能包含两个连续的逗号,比如"1,,3"。
示例 1:
"9,3,4,#,#,1,#,#,2,#,6,#,#"
返回 true
示例 2:
"1,#"
返回 false
示例 3:
"9,#,#,1"
返回 false
详见:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/description/
C++:
- class Solution {
- public:
- bool isValidSerialization(string preorder) {
- istringstream in(preorder);
- vector<string> v;
- string t = "";
- int cnt = 0;
- while (getline(in, t, ','))
- {
- v.push_back(t);
- }
- for (int i = 0; i < v.size() - 1; ++i)
- {
- if (v[i] == "#")
- {
- if (cnt == 0)
- {
- return false;
- }
- --cnt;
- }
- else
- {
- ++cnt;
- }
- }
- return cnt == 0 && v.back() == "#";
- }
- };
参考:https://www.cnblogs.com/grandyang/p/5174738.html
331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化的更多相关文章
- [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, ...
- 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, ...
- 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 ...
- 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 ...
随机推荐
- 关于python字典中文显示的处理办法
最近工作中遇到字典包含中文,显示\uxxxx的问题,怎么转换都无法输入正常的中文:{"gc": "\u4eba\u751f\u7f8e\u597d", &quo ...
- noip模拟赛 柜(暴力)
分析:暴力的方法是非常显然的,从起点走一次,从终点走一次,路径相交的点即为所求,但是这样存图都很难存下,而且如果数据极端可能要求R*C次,时间空间都受不了.如果不需要记录整张图,并且一次能移动很多步就 ...
- 简单svg动画
一.将svg嵌入到html中 svg是指可伸缩矢量图形,它使用XML格式定义图像.在html中可以使用<svg>标签直接嵌入svg代码,例如: <svg version=" ...
- vim下多行注释与解注释
1.多行注释 (1)按esc进入命令行模式 (2)按下Ctrl+v,进入区块模式,并使用上下键选择需要注释的多行 (3)按下“I”(大写)键,进入插入模式 (4)输入注释符(“//”或“#”等) (5 ...
- 关于使用CELERY的一点心得
使用也有大半年了.稳定性没话说啊. 但有一个坑,是我以前没注意的,记录下来. 就是本来一个任务是可以异步并行执行的..但如何需要CELERY的执行结果来作判断的话,就会变得异步串行的. 这要值得注意. ...
- Regular Number 字符串匹配算法 Shift_and
Using regular expression to define a numeric string is a very common thing. Generally, use the shape ...
- JavaScript 读取CSV文件并转为js对象
html部分 <!-- 创建隐藏file input --><button type="button" name="seach" onclic ...
- 在net中json序列化与反序列化 面向对象六大原则 (第一篇) 一步一步带你了解linq to Object 10分钟浅谈泛型协变与逆变
在net中json序列化与反序列化 准备好饮料,我们一起来玩玩JSON,什么是Json:一种数据表示形式,JSON:JavaScript Object Notation对象表示法 Json语法规则 ...
- LeetCode 141. Linked List Cycle (链表循环)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- HDU 2461 线段树扫描线
给出N个矩形,M次询问 每次询问给出R个.问这R个矩形围成的面积 经典扫面线求面积并,对每次询问的R个点离散化一下 #include "stdio.h" #include &quo ...