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 ...
随机推荐
- list tuple dict (列表,元祖,字典间的相互转换)
#-*- coding:UTF-8 -*- #author:RXS002 #1.字典 dict = {'name':'Zara','age':7,'class':'First'} #字典转换为字符串, ...
- Spring 事务XML配置
<!-- 配置 Spring 的声明式事务 --> <!-- 1. 配置事务管理器 --> <bean id="transactionManager" ...
- poj 3164 最小树形图模板!!!
/* tle十几次,最后发现当i从1开始时,给环赋值时要注意啊! 最小树形图 */ #include<stdio.h> #include<string.h> #include& ...
- CodeForces - 459C - Pashmak and Buses
先上题目+: C. Pashmak and Buses time limit per test 1 second memory limit per test 256 megabytes input s ...
- 遇到很多次,要注意区分service调用,本地用户调用这些区别
WTSQueryUserToken返回1314 The WTSQueryUserToken function obtains the primary access token of the log ...
- ECMAScript 6 入门学习笔记(二)——变量的解构赋值
一.数组的解构赋值 let [foo, [[bar], baz]] = [1, [[2], 3]]; ①可多不可少,等号的右边是数组 let [x, y] = [1, 2, 3]; //可以 let ...
- [CH#56]过河(贪心)
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2356%20-%20%E5%9B%BD%E5%BA%86%E8%8A%82%E6%AC%A2%E4%B9%90% ...
- Mysql 使用delete drop truncate 删除数据时受外键约束影响解决方案
先禁用数据库的外键约束: set foreign_key_checks=0; 进行删除操作 delete.drop.truncate 恢复数据库外键约束: set foreign_key_checks ...
- 2102 石子归并 2codevs
2102 石子归并 2codevs 题目描述 Description 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为 ...
- SecureCRT复制粘贴快捷键
复制:[Ctrl]+[Insert] 粘贴:[Shift]+[Insert]