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 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
|
150 / 150 test cases passed.
|
Status:
Accepted |
|
Runtime: 8 ms
|
class Solution {
public:
bool isValidSerialization(string preorder) {
int l = preorder.length();
if(preorder[] == '#'){
if(l == ) return true;
else return false;
}
int i = ;
stack<int> s;
s.push();
while(i<l && preorder[i] != ',') i++;
i++;
int te;
while(i < l)
{
if(preorder[i] == '#'){
if(s.empty() == ) return false;
while(!s.empty())
{
te = s.top();
if(te == ) return false;
s.pop();
s.push(te + );
te = s.top();
if(te == ){
break;
}
if(te == ){
s.pop();
}
}
if(s.empty() == && i < l - ) return false;
}
else{
s.push();
}
while(i < l && preorder[i] != ',') i++;
i++;
}
if(s.size() == ) return true;
return false;
}
};
leetcode 331. Verify Preorder Serialization of a Binary Tree的更多相关文章
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【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 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, ...
- [LeetCode] 331. Verify Preorder Serialization of a Binary Tree_Medium tag: stack
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
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 验证二叉树的前序序列化
序列化二叉树的一种方法是使用前序遍历.当我们遇到一个非空节点时,我们可以记录这个节点的值.如果它是一个空节点,我们可以使用一个标记值,例如 #. _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 ...
随机推荐
- 在idea下创建maven
之前一直用eclipse,现在要用idea写一个安装过程玩玩 一:New Project 二:选择maven,在project SDK上选择你安装的jdk,默认安装在c:/Program Files ...
- Noip2016 提高组 蚯蚓
刚看到这道题:这题直接用堆+模拟不就可以了(并没有认真算时间复杂度) 于是用priority_queue水到了85分-- (STL大法好) 天真的我还以为是常数问题,于是疯狂卡常--(我是ZZ) 直到 ...
- javaEE(3)_servlet基础
一.Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术,Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序 ...
- [LUOGU] 3959 宝藏
https://www.luogu.org/problemnew/show/P3959 注意到n非常小,考虑状压/搜索. 发现状压需要枚举起点,跑n次,一个问题是转移不可以以数字大小为阶段了,考虑用d ...
- Git学习——创建本地仓库、提交文件
创建Git仓库 新建或找一个存在的文件夹,在命令行进入该文件夹,输入命令 git init 添加文件到Git仓库 首先使用命令git add <file>,可以多次添加文件: 使用命令gi ...
- windows显示文件扩展名
搜索打开windows的文件资源管理选项,如下去掉“隐藏已知文件类型的扩展名”即可 打开之后显示如下:
- Linux中文件压缩与解压
压缩与解压 compress 文件名 1 -v //详细信息 2 3 -d //等于 uncompress 默认只识别 .Z 如果使用别的后缀,会导致不识别,解压缩失败.也可以使用 -d -c 压缩包 ...
- 本地开发环境中部署已经写好的magento2.0项目
环境:apache2.4.25+php7.0.16+mysql5.7 (注意版本搭配,详细可以看magento2.0官网看配置) apache最好使用80端口,host文件配置本地虚拟域名 php.i ...
- mysql 审核引擎 goInception 的基本使用
官网地址 github.com 安装 git clone https://github.com/hanchuanchuan/goInception.git cd goInception 修改配置 开启 ...
- 使用VS2015编写驱动时出现的部分错误以及解决方法
前几日在github上下载了一个Windows驱动的demo,原本想着直接下载下来打开解决方案就可以用,没想到一编译,出现了奇奇怪怪的Error,部分Error网上也没什么好的解决办法,对我这个从未深 ...