[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, 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:
Input:"9,3,4,#,#,1,#,#,2,#,6,#,#"
Output:true
Example 2:
Input:"1,#"
Output:false
Example 3:
Input:"9,#,#,1"
Output:false这个题目的思路参考Solution, 本质上就是看这个tree 是否valid, 然后就要满足可以有的children数>= 0 , 最后== 0, 那么最开始slot = 1, 然后for loop, 每
多一个数字, 表明可以多一个child, 如果是#表明少一个child, 最后children数等于0 即可. 就是用Stack,每次碰到#, 去看stack[-1]是否为#, 如果是的话表明stack[-2]的children满了, 所以将stack.pop()执行两次, 并且循环, 然后把#放到stack里面, 这里在stack.pop()两次
的中间, 加入一个stack 非空的判断, 是针对"##" "###"这样的情况来的.最后len(stack) == 1 and stack[-1] == '#' Code: T: O(n) S: O(n)
class Solution:
def preorderValid(self, preorder):
stack, preorder = [], preorder.split(',')
for p in preorder:
while (p == '#' and stack and stack[-1] == '#'):
stack.pop()
if not stack: return False
stack.pop()
stack.append(p)
return len(stack) == 1 and stack[-1] == '#'
Code: T: O(n) S: O(1)
class Solution:
def preorderValid(self, preorder):
slot, preorder = 1, preorder.split(',')
for p in preorder:
if slot == 0: return False # means that should have no children anymore
if p == '#':
slot -= 1
else:
slot += 1
return slot == 0
[LeetCode] 331. Verify Preorder Serialization of a Binary Tree_Medium tag: stack的更多相关文章
- 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】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
题目如下: 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 ...
随机推荐
- 关于SQL优化(转载,格式有调整)
一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...
- Javascript常见性能优化
俗话说,时间就是生命,时间就是金钱,时间就是一切,人人都不想把时间白白浪费,一个网站,最重要的就是体验,而网站好不好最直观的感受就是这个网站打开速度快不快,卡不卡. 当打开一个购物网站卡出翔,慢的要死 ...
- Autojump:一个可以在 Linux 文件系统快速导航的高级 cd 命令
相关博客:https://linux.cn/article-3401-1.html 对于那些主要通过控制台或终端使用 Linux 命令行来工作的 Linux 用户来说,他们真切地感受到了 Linux ...
- 去除select边框和三角-----appearance:none
今天发现一个比较有意思的属性,appearance:none 可能有朋友不认识,但是有一个标签你肯定认识:select. 这个标签的样式是这样的: 一般情况下,我们所使用的border:0; 去除边框 ...
- 【转】Android四大基本组件介绍与生命周期
转自:http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html Android四大基本组件分别是Activity,Serv ...
- Packetbeat协议扩展开发教程(1)
Packetbeat ( https://www.elastic.co/products/beats/packetbeat )是一个开源的网络抓包与分析框架,内置了很多常见的协议解析,如HTPP.My ...
- vim自定义语法高亮(syntax highlight)设置流程
这里用一个非常简单的例子来展示vim自定义自己的语法并高亮显示的设置流程. 所使用的实例语言是python,为了区分vim自带的python语法,我们把文件名保存为test.me,其内容如下图所示: ...
- 【转】.NET Framework、C#语言、IDE、CLR 版本历史及其差异
原文地址: http://www.cnblogs.com/PurpleCow/archive/2012/06/17/2552780.html http://www.cnblogs.com/lhking ...
- MyBatis 强大之处 多环境 多数据源 ResultMap 的设计思想是 缓存算法 跨数据库 spring boot rest api mybaits limit 传参
总结: 1.mybaits配置工2方面: i行为配置,如数据源的实现是否利用池pool的概念(POOLED – This implementation of DataSource pools JDBC ...
- CyanogenMod---android
http://blog.csdn.net/sheldon4090/article/details/7736957--service. android进程间通信:使用AIDL http://blog.c ...