当前位置:网站首页>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);
}
};
边栏推荐
- Almost all known protein structures in the world are open sourced by DeepMind
- 【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
- 实际工作中的高级技术(训练加速、推理加速、深度学习自适应、对抗神经网络)
- 技术分享| 融合调度系统中的电子围栏功能说明
- CF1527D MEX Tree(mex&树&容斥)
- leetcode:241. 为运算表达式设计优先级
- [Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
- 如何确定异步 I/O 瓶颈
- 异步编程概览
- leetcode: 253. How many meeting rooms are required at least
猜你喜欢

【模型部署与业务落地】基于量化芯片的损失分析

Rust from entry to proficient 04-variables

CCF GLCC正式开营|九州云开源专家携丰厚奖金,助力高校开源推广

leetcode:241. 为运算表达式设计优先级

eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)

技术分享| 小程序实现音视频通话

Qt的QItemDelegate使用

Compound Refractive Lenses for X-ray Focusing

Leetcode: 215 disorderly to find the first big k element in the array

特殊品种的二次开户验资金额
随机推荐
【Web技术】1401- 图解 Canvas 入门
华为云 & 达达,帮有情人“一键送达”
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
杭电校赛(逆袭指数)
Sum of four squares, laser bombs
【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
How to automatically renew the token after it expires?
数据链路层-------以太网协议
Why does the decimal point appear when I press the space bar in word 2003?
idea removes spark logs
【问题解决】QT更新组件出现 “要继续此操作,至少需要一个有效且已启用的储存库”
X射线掠入射聚焦反射镜
[Problem solving] QT update component appears "To continue this operation, at least one valid and enabled repository is required"
【模型部署与业务落地】基于量化芯片的损失分析
Go 语言快速入门指南: 变量和常量
【剑指offer59】队列的最大值
Bluetooth Technology|In the first half of the year, 1.3 million charging piles were added nationwide, and Bluetooth charging piles will become the mainstream of the market
Find My技术|防止你的宠物跑丢,苹果Find My技术可以帮到你
2042. 检查句子中的数字是否递增-力扣双百代码-设置前置数据
FRED Application: Capillary Electrophoresis System