当前位置:网站首页>leetcode: 255 Verify preorder traversal sequence binary search tree
leetcode: 255 Verify preorder traversal sequence binary search tree
2022-08-04 14:43:00 【OceanStar's study notes】
题目来源
题目描述
给定一个整数数组,你需要验证它是否是一个二叉搜索树正确的先序遍历序列.
你可以假定该序列中的数都是不相同的.
参考以下这颗二叉搜索树:
示例 1:
输入: [5,2,6,1,3]
输出: false
示例 2:
输入: [5,2,1,3,6]
输出: true
题目解析
思路
- 二叉搜索树的特点是: 左子树的值 < 根节点的值 < 右子树的值
- So the in-order traversal must be an ordered array,But the former sequence traversal what law?Its preorder traversal array is locally decreasing,Increasing the overall array
- 所以可以借助单调栈
- set a minimum value low,然后遍历数组,If the current value is less than this minimum low,返回 false
- 对于根节点,将其压入栈中,然后往后遍历
- If the number encountered is less than the top element of the stack,Description is the point of its left subtree,继续压入栈中
- until the encountered number is greater than the top element of the stack,Then the value on the right is.Which is need to find the right child node tree,所以更新 low value and delete the top element of the stack,Then continue to compare with the next top element of the stack,如果还是大于,则继续更新 low Value and cut out the top,Until the stack is empty or greater than the current value to stop the current stack elements,push current value
- In this way, if it does not return before traversing the entire array false 的话,最后返回 true 即可
class Solution {
public:
bool verifyPreorder(vector<int>& preorder) {
if(preorder.size() <= 2) return true;
int MIN = INT32_MIN;
stack<int> s;
for(int i = 0; i < preorder.size(); ++i)
{
if(preorder[i] < MIN)
return false;
while(!s.empty() && s.top() < preorder[i])//met a big one,右分支
{
MIN = s.top();//The top of the record stack is the minimum value
s.pop();
}
s.push(preorder[i]);
}
return true;
}
};
进阶要求
In order to keep the space complexity constant,我们不能使用 stack,所以直接修改 preorder,将 low 值存在 preorder specific location
class Solution {
public:
bool verifyPreorder(vector<int>& preorder) {
int low = INT_MIN, i = -1;
for (auto a : preorder) {
if (a < low) return false;
while (i >= 0 && a > preorder[i]) {
low = preorder[i--];
}
preorder[++i] = a;
}
return true;
}
};
分治法
- Maintain a lower bound in the recursive functions lower 和上界 upper,Then the currently traversed node must be in(low, upper)之间
- Then search for the first one in the given interval>=point of current value,以此为分界点,Call the recursive function on the left and right sides respectively.Note the left half upper Update to current node value val,Indicates that the node value of the left subtree must be less than the current node value,while the right half of the recursive lower Update to current node value val,Indicates that the node value of the right subtree must be greater than the current node value
- If both the left and right parts return true,returns true as a whole
class Solution {
public:
bool verifyPreorder(vector<int>& preorder) {
return helper(preorder, 0, preorder.size() - 1, INT_MIN, INT_MAX);
}
bool helper(vector<int>& preorder, int start, int end, int lower, int upper) {
if (start > end) return true;
int val = preorder[start], i = 0;
if (val <= lower || val >= upper) return false;
for (i = start + 1; i <= end; ++i) {
if (preorder[i] >= val) break;
}
return helper(preorder, start + 1, i - 1, lower, val) && helper(preorder, i, end, val, upper);
}
};
边栏推荐
- 兆骑科创创新创业大赛活动举办,线上直播路演,投融资对接
- 技术分享| 小程序实现音视频通话
- CF1527D MEX Tree (mex & tree & inclusive)
- NPDP|作为产品经理,如何快速提升自身业务素养?
- This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
- Win10无法访问移动硬盘怎么解决
- 如何和程序员谈恋爱
- MySQL【窗口函数】【共用表表达式】
- 第十六章 源代码文件 REST API 教程(一)
- Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
猜你喜欢
metaRTC5.0新版本支持mbedtls(PolarSSL)
FRED应用:毛细管电泳系统
1403. 非递增顺序的最小子序列
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
基于 Next.js实现在线Excel
CloudCompare&PCL 点云按网格划分(点云分幅)
基本介绍PLSQL
世间几乎所有已知蛋白质结构,都被DeepMind开源了
编译型与解释型编程语言的区别
Zheng Qing freshmen school competition and middle-aged engineering selection competition
随机推荐
Almost all known protein structures in the world are open sourced by DeepMind
1401 - Web technology 】 【 introduction to graphical Canvas
Makefile 语法及使用笔记
Makefile syntax and usage notes
16、学习MySQL 正则表达式
G.登山小分队(暴力&dfs)
理论篇1:深度学习之----LetNet模型详解
爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例
Workaround without Project Facets
第十六章 源代码文件 REST API 教程(一)
属于程序猿的浪漫
idea removes spark logs
How to write SQL statements: the usage of Update, Case, and Select together
leetcode: 212. Word Search II
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
LeetCode_模拟_中等_498.对角线遍历
leetcode: 253. How many meeting rooms are required at least
Problem solving-->Online OJ (18)
[深入研究4G/5G/6G专题-50]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-10-高可靠性技术-1-低编码率编码调制方案MCS与高可靠性DRB
Redis 复习计划 - Redis主从数据一致性和哨兵机制