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 ...
随机推荐
- Google Colab调用cv2.imshow奔溃
当我在Google Colab运行如下代码 import cv2 import numpy as np image = cv2.imread('a.jpg') cv2.imshow('original ...
- VGG16学习笔记
转载自:http://deanhan.com/2018/07/26/vgg16/ 摘要 本文对图片分类任务中经典的深度学习模型VGG16进行了简要介绍,分析了其结构,并讨论了其优缺点.调用Keras中 ...
- pb2.text_format.Merge(f.read(), self.solver_param) AttributeError: 'module' object has no attribute 'text_format'
http://blog.csdn.net/qq_33202928/article/details/72526710
- gson对象的相互转换
参见 http://www.javacreed.com/gson-deserialiser-example/
- objdump命令
0x00 objdump命令是Linux下的反汇编目标文件或者可执行文件的命令 0x01 objdump -f 显示test的文件头信息 $ objdump -f levellevel: file ...
- PHP 递归无限极下级
下面是自己用到的一些递归方法,当然都是借鉴的,各位看官请勿怪 第一种 有层级 $array = array( array('id' => 1, 'pid' => 0, 'n' => ...
- 121. Best Time to Buy and Sell Stock@python
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- js截屏
<html><head> <meta name="layout" content="main"> <meta http ...
- JS与IOS、安卓的交互
最近做的项目中涉及到了与安卓和ios的交互问题,对于一个新手来说,多多少少会有点迷糊.在调用安卓和ios的callback回调时,很轻松的就调用成功了,而且,步骤也不那么繁琐.刚开始,只知道那样使用可 ...
- 【Java_多线程并发编程】JUC原子类——AtomicLong原子类
1. AtomicLong是基本原子类中的一种 AtomicLong是对长整形进行原子操作. 1.1 AtomicLong类的函数列表 // 构造函数 AtomicLong() // 创建值为init ...