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的更多相关文章

  1. leetcode 331. Verify Preorder Serialization of a Binary Tree

    传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...

  2. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  3. 【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 ...

  4. 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, ...

  5. 【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 ...

  6. 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, ...

  7. 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, ...

  8. 331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化

    序列化二叉树的一种方法是使用前序遍历.当我们遇到一个非空节点时,我们可以记录这个节点的值.如果它是一个空节点,我们可以使用一个标记值,例如 #.     _9_    /   \   3     2  ...

  9. LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27

    331. 验证二叉树的前序序列化 331. Verify Preorder Serialization of a Binary Tree 题目描述 每日一算法2019/5/30Day 27LeetCo ...

随机推荐

  1. 一些有用的java 框架

    jwt  用于生成web toke的类库 http://jwt.io/ jasypt java加密类库 http://www.jasypt.org/

  2. 【Spring源码分析系列】搭建Spring实现容器的基本实现

    前言 bean是Spring中最核心的东西,因为Spring就像一个大水桶,而bean就像是容器中的水,先新建一个小例子来看一下: 一.使用eclipse构建项目,项目结构如下 二.类文件内容 < ...

  3. 异构GoldenGate 12c 单向复制配置(支持DDL复制)

    1.开始配置OGG支持DDL复制(在source端操作) 1.1 赋予权限 SQL> conn /as sysdba 已连接. SQL> grant execute on utl_file ...

  4. 题目1459:Prime ring problem(素数环问题——递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  5. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  6. 遍历json数组实现树

    今天小颖在工作中遇到要遍历树得问题了,实现后,怕后期遇到又忘记啦,所以记录下嘻嘻,其实这个和小颖之前写过得一篇文章    json的那些事    中第4点有关json的面试题有些类似. 数组格式: v ...

  7. sencha touch 在线实战培训 第一期 第三节

    2014.1.2晚上8点开的课 讲课进度比较快,好多同学反应说有些跟不上了... 呃,本期的课程是需要有一定的基础的. 建议大家多看看http://www.cnblogs.com/mlzs/p/346 ...

  8. sencha touch NavigationView 源码详解(注释)

    Ext.define('Ext.navigation.View', { extend: 'Ext.Container', alternateClassName: 'Ext.NavigationView ...

  9. H3C系列之三层交换机文件管理

    笔者本篇文章所用h3c交换机的型号为三层交换机S3600-28TP-SI 对于文件的操作一般都在用户视图下操作,常见的有如下一些操作: 1.查看操作,常用的查看操作可以使用如下命令: <H3C& ...

  10. 【CF913G】Power Substring 数论+原根

    [CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一 ...