当前位置:网站首页>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版本的入度出度方法的速度也还不错,但应该是有更多的方法可以进一步提速的,希望朋友们能够多多指教,非常感谢。
边栏推荐
- How to conduct TPC-C test on tidb
- 03_线性表_链表
- LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
- 16_Redis_Redis持久化
- 哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
- 11_Redis_Hyperloglog_命令
- I made an istio workshop. This is the first introduction
- Application and practice of Jenkins pipeline
- 表格响应式布局小技巧
- 如何用 Sysbench 测试 TiDB
猜你喜欢

There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant

Application and practice of Jenkins pipeline

List set & UML diagram

. Solution to the problem of Chinese garbled code when net core reads files

Markdown tutorial

18_ Redis_ Redis master-slave replication & cluster building

Yolo format data set processing (XML to txt)

编译原理课程实践——实现一个初等函数运算语言的解释器或编译器

IE 浏览器正式退休

Set set you don't know
随机推荐
Why can't programmers who can only program become excellent developers?
kibana 基础操作
解决el-radio-group 回显后不能编辑问题
Download blender on Alibaba cloud image station
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
. Net core logging system
Implementation of n queen in C language
IE 浏览器正式退休
Tidb data migration tool overview
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
YOLOV5 代码复现以及搭载服务器运行
[solution] educational codeforces round 82
Mavn builds nexus private server
Base64 编码原来还可以这么理解
牛客练习赛101
How to solve the problem of database content output
03_線性錶_鏈錶
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
20_Redis_哨兵模式
Base64 coding can be understood this way