- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/description/
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.
Youmay 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
给了一个先序遍历,判断能不能构成合法的二叉树。这个字符串中,#表示空节点。
我们的思路应该是这样的:判断一个二叉树是否合法的情况,那么应该是个递归或者循环问题。那么解决问题的思路是从顶部向下分析还是从底下向顶部分析呢?正确的结果应该是从二叉树的底部向上进行分析,因为我们可以通过#号判断是否是空节点,然后判断最底下的叶子节点是否含有两个空孩子的方式,循环向上解决这个问题。
所以这个题的思路就很明了了:用一个栈,从字符串的左侧向右依次进栈,如果满足栈的后三位是数字,#,#的模式时,说明可以构成合法的叶子节点,把这个叶子节点换成#号,代表空节点,然后继续遍历。最后应该只剩下一个#,那么就是一个合法的二叉树。
如:”9,3,4,#,#,1,#,#,2,#,6,#,#” 遇到x的时候,就把它变为
模拟一遍过程:
9,3,4,#,# => 9,3,# 继续读
9,3,#,1,#,# => 9,3,#,# => 9,# 继续读
9,#2,#,6,#,# => 9,#,2,#,# => 9,#,# =>
class Solution(object):
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
stack = []
for node in preorder.split(','):
stack.append(node)
while len(stack) >= 3 and stack[-1] == stack[-2] == '#' and stack[-3] != '#':
stack.pop(), stack.pop(), stack.pop()
stack.append('#')
return len(stack) == 1 and stack.pop() == '#'
1 2 3 4 5 6 7 8 9 10 11 12 13
方法二:
这个方法还是第一次使用:看出度和入度的差。
我们知道一个树(甚至图),所有节点的入度之和等于出度之和。那么可以根据这个条件进行有效性的判断。
对于二叉树,我们把空的地方也作为叶子节点(如题目中的#),那么有
1、 所有的非空节点提供2个出度和1个入度(根除外);
2、 所有的空节点但提供0个出度和1个入度;
我们在遍历的时候,计算diff = outdegree – indegree. 当一个节点出现的时候,diff – 1,因为它提供一个入度;当节点不是#的时候,diff+2(提供两个出度) 如果序列式合法的,那么遍历过程中diff >=0 且最后结果为0.
这里解释一下为什么diff的初始化为1.因为,我们加入一个非空节点时,都会先减去一个入度,再加上两个出度。但是由于根节点没有父节点,所以其入度为0,出度为2.因此diff初始化为1,是为了再加入根节点的时候,先减去一个入度,再加上两个出度,正好应该是2.
class Solution(object):
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
nodes = preorder.split(',')
diff = 1
for node in nodes:
diff -= 1
if diff < 0:
return False
if node != '#':
diff += 2
return diff == 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
参考:https://www.hrwhisper.me/leetcode-verify-preorder-serialization-of-a-binary-tree/
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
目前我有以下内容: $.ajax({ type: 'POST', url: this.action, data: $(this).serialize(), }); 这工作正常,
目前我有以下内容: $.ajax({ type: 'POST', url: this.action, data: $(this).serialize(), }); 这很好用,但
我知道什么是序列化,但对我来说,这是一个无法描述其含义的术语。 为什么我们称序列化为序列化?将对象转换为原始数据(以及膨胀/反序列化,就此而言)有什么意义?谁创造了这个术语,为什么? 最佳答案 它可能
是否可以将数据结构(使用 boost::serialization)序列化为字符串变量或缓冲区(而不是磁盘上的文件)? 最佳答案 当然,让它在stringstream上完成工作。 关于serializ
假设我有以下类型定义,它依赖于常量来指示记录成员的向量长度: type point_t is record x: std_logic_vector(X_WIDTH-1 downto 0);
我尝试序列化一个向量和一个 map 容器,并通过 cout 输出它们的值。然而,我很难理解boost输出的含义。我的代码如下所示: #include #include #include #
我正在尝试将序列化功能添加到我的 Rust 结构之一。这是一个日历事件,看起来像这样: #[derive(PartialEq, Clone, Encodable, Decodable)] pub st
正如主题所暗示的那样,在将大量数据序列化到文件时,我遇到了 boost::serialization 的一个小问题。问题在于应用程序的序列化部分的内存占用大约是被序列化对象内存的 3 到 3.5 倍。
在搜索解决方案时,我得到了 this和 this但我不清楚这个概念,所以无法实现:(。当我尝试更新数据库中的值(特别是日期时间对象)时会发生此错误。以下是我正在使用的代码:- var upd
我收到以下错误, 模板对象不可迭代 def get_AJAX(request, id): data = serializers.serialize("json", Template.objec
由于方便,我正在考虑对我的所有数据 i/o 使用 serialize() 和 deserialize()。但是,我不想在 Julia 更新中被不可读的文件所困扰。 serialize() 和 dese
我有一个通常使用 JMS Serializer 包序列化的实体。我必须在序列化中添加一些不驻留在实体本身中但通过一些数据库查询收集的字段。 我的想法是创建一个自定义对象,用实体字段填充字段并添加自定义
我正在尝试使用 XmlParser 从 xml 文件中删除和添加标签。以下是我在使用“grails run-app”命令部署的 grails 应用程序中执行时运行良好的代码块: def parser
我正在使用 MRUnit 测试 MultipleOutputs。测试用例失败并显示以下消息。 java.lang.ClassCastException: org.apache.hadoop.io.se
本文整理了Java中com.jme3.network.serializing.serializers.ZIPSerializer类的一些代码示例,展示了ZIPSerializer类的具体用法。这些代码
我有一个处理草图,需要与 USB 设备建立 2 个连接。我无法提前判断哪个设备是 USB0 哪个是 USB1。 (不是我至少知道) 其中一台设备发出问候语,另一台设备根本不回答。因此,我编写了带有简单
我在下面有这个代码,我来自 this forum我遵循了。它对我不起作用,但他们声称代码很好。我已经尝试了几种字符串比较方法,例如 string.equals(string)和标准==运营商,仍然没有
当我尝试调用特定的 Web 服务方法时,我收到“Unspecified error”。使用 XMLSpy 我发现参数对象还没有被序列化。 在生成的序列化程序源中,我注意到以下几行: if (!need
在 Rust 中编写 NEAR 智能合约,我的编译器似乎要求通过 API 发送的对象具有 Serialize trait,以及存储在区块链中的对象 BorshSerialize和 BorshDeser
我正在尝试 Kotlin 序列化。按照说明进行设置后,我得到了 Unresolved reference: serializer使用此代码构建错误: val serializer : KSeriali
我是一名优秀的程序员,十分优秀!