当前位置:网站首页>LeetCode刷题——验证二叉树的前序序列化#331#Medium
LeetCode刷题——验证二叉树的前序序列化#331#Medium
2022-07-02 12:04:00 【喷火龙与水箭龟】
验证二叉树的前序序列化的思路探讨与源码
验证二叉树的前序序列化的题目如下图,该题属于栈类和二叉树类型的题目,主要考察对于搜索方法的使用和树结构的理解。本文的题目作者想到2种方法,分别是栈方法和入度出度方法,其中栈方法使用Java进行编写,而入度出度方法使用Python进行编写,当然这可能不是最优的解法,还希望各位大佬给出更快的算法。
本人认为该题目可以使用栈方法的思路进行解决,首先计算字符串的长度,并初始化一个双端队列,并压入一个元素1。开始遍历循环,当双端队列是空的时候就直接返回否的结果,当遇到字符值是逗号就直接跳过,当字符值是’#'的时候就将双端队列的头部元素取出并减去1,再判断是否大于0,如果是则放回双端队列的头部;否则就将栈顶元素减去1后再压入一个2,以此遍历循环,直到最后遍历结束判断双端队列是否为空并返回判断结果。那么按照这个思路我们的Java代码如下:
#喷火龙与水箭龟
class Solution {
public boolean isValidSerialization(String preorder) {
int preorderLen = preorder.length();
int ir = 0;
Deque<Integer> stackList = new LinkedList<Integer>();
stackList.push(1);
while (ir < preorderLen) {
if (stackList.isEmpty()) {
return false;
}
if (preorder.charAt(ir) == ',') {
ir = ir + 1;
} else if (preorder.charAt(ir) == '#'){
int topNum = stackList.pop() - 1;
if (topNum > 0) {
stackList.push(topNum);
}
ir = ir + 1;
} else {
while (ir < preorderLen && preorder.charAt(ir) != ',') {
ir = ir + 1;
}
int topNum = stackList.pop() - 1;
if (topNum > 0) {
stackList.push(topNum);
}
stackList.push(2);
}
}
return stackList.isEmpty();
}
}
显然,我们看到栈方法的效果还可以,同时还可以使用入度出度的方法解决。首先按英文逗号将字符串进行分割得到数组,初始化一个标记值。开始遍历循环数组,将标记值减去1,然后判断标记值是否小于0,如果是就直接返回否的结果。再次判断 当前字符值是否不等于’#'的值,如果是就将标记值加上2,按照这个思路进行循环直到遍历结束,最终判断标记值是否等于0,并返回判断结果。所以按照这个思路就可以解决,下面是Python代码:
#喷火龙与水箭龟
class Solution(object):
def isValidSerialization(self, preorder):
vex = preorder.split(',')
resNum = 1
for v in vex:
resNum = resNum - 1
if(resNum < 0):
return False
if(v != '#'):
resNum = resNum + 2
return resNum == 0
从结果来说Java版本的栈方法的效率还可以,而Python版本的入度出度方法的速度也还不错,但应该是有更多的方法可以进一步提速的,希望朋友们能够多多指教,非常感谢。
边栏推荐
- I made an istio workshop. This is the first introduction
- Table responsive layout tips
- Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
- Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- TiDB跨数据中心部署拓扑
- Deploy tidb cluster with tiup
- AtCoder Beginner Contest 254
- Solution of Queen n problem
- XML配置文件
猜你喜欢
15_ Redis_ Redis. Conf detailed explanation
16_Redis_Redis持久化
Practical debugging skills
.NET Core 日志系统
. Solution to the problem of Chinese garbled code when net core reads files
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
Map介绍
Tidb data migration tool overview
可视化搭建页面工具的前世今生
Kibana basic operation
随机推荐
Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
Practical debugging skills
XML配置文件
15_Redis_Redis.conf详解
19_Redis_宕机后手动配置主机
GeoServer offline map service construction and layer Publishing
10_Redis_geospatial_命令
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
List set & UML diagram
Case introduction and problem analysis of microservice
TiDB 环境与系统配置检查
TiDB 集群最小部署的拓扑架构
07_ Hash
Application and practice of Jenkins pipeline
C语言实现N皇后问题
TiDB跨数据中心部署拓扑
Data analysis thinking analysis methods and business knowledge - business indicators
17_Redis_Redis发布订阅
学习使用php实现公历农历转换的方法代码
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)