当前位置:网站首页>"98 BST and Its Verification" of the 13th day of leetcode brushing series of binary tree series
"98 BST and Its Verification" of the 13th day of leetcode brushing series of binary tree series
2022-08-11 03:51:00 【small machine double】
98 BST及其验证
BSTBinarybinary search tree,二叉搜索树,Also known as a binary sorted tree,When using in-order traversal on it, you can get an increasing sequence.
题目描述
给定一个二叉树,判断其是否是一个有效的二叉搜索树
假设一个二叉搜索树具有如下特征:
节点的左子树只包含小于当前节点的数.
节点的右子树只包含大于当前节点的数.
所有左子树和右子树自身必须也是二叉搜索树.
示例
输入:
2
/
1 3输出:true
输入:
5
/ \
1 4
/ \
3 6
输出:false
解释:根节点的值为5,But the value of its right child is 4,The condition that the value of all nodes in the right subtree is greater than the root node is not satisfied
题目解析
Depth-first search based on topic prompts,It can be written recursively.It is enough to judge each time whether the left and right subtrees satisfy the conditions of the binary search tree;
All nodes of the left subtree have values less than the value of the root node
The value of all nodes in the right subtree is greater than the value of the root node
But it's not just comparing the values of the root node and the left and right nodes to meet the conditions,It is to satisfy the condition on the entire binary tree,Therefore, there should be a bounded range for the value of each node,例如:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hCymzVOj-1601715451019)(D:\我的博客\刷题\img\leetCode98图示1.png)]
So return when recursivefalseYou can't just compare the size of the left and right nodes with the parent node,How to do it is only understood after reading the code of the big guy,代码如下:
public boolean isBST(TreeNode root, long min, long max) {
if (root == null) {
return true;
}
if (root.val <= min || root.val >= max) {
return false;
}
return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
return isBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
实现思路如下:
First use a maximum value and a minimum value as the boundary judgment conditions,Make sure that the root node value of the entire tree is within this range,这里使用LongThe maximum value of the type is because there is a test case when the code is submittedint类型的最大值,导致错误.
The program enters firstisBST函数时,对于
root.val <= min || root.val >= max
该条件的bool值为真.Then enter the following recursive condition.
And this recursion is also set very concise,for the node of the left subtree,The minimum value of its bounds is unchanged,The maximum value is set to the value of the current node,That is, the node of the left subtree cannot be greater than the value of the current node;for nodes in the right subtree,The maximum value of its bounds is unchanged,The minimum value is set to the value of the current node,That is, the node of the right subtree cannot be smaller than the value of the current node.
The judgment condition judges whether the scope of each node is in(min, max),Returns beyond this rangefalse,The constraints of the boundary conditions are given by the parameters passed during the recursion.
程序执行结果:
参考:小浩算法-BST及其验证
边栏推荐
- typedef定义结构体数组类型
- 电力机柜数据监测RTU
- Echart地图的省级,以及所有地市级下载与使用
- console.log alternatives you didn't know about
- LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
- Qnet弱网测试工具操作指南
- The FTP error code list
- The "top pillar" slides, and new growth is extremely difficult to shoulder the heavy responsibility. Is Ali "squatting" to jump higher?
- 【力扣】22.括号生成
- STC8H development (15): GPIO drive Ci24R1 wireless module
猜你喜欢
【FPGA】day22-SPI protocol loopback
LeetCode刷题第16天之《239滑动窗口最大值》
高校就业管理系统设计与实现
Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
【愚公系列】2022年08月 Go教学课程 036-类型断言
机器学习中什么是集成学习?
The custom of the C language types -- -- -- -- -- - structure
【C语言】入门
QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
【FPGA】day19-二进制转换为十进制(BCD码)
随机推荐
Multi-serial port RS485 industrial gateway BL110
typedef defines the structure array type
Binary tree related code questions [more complete] C language
Enter the starting position, the ending position intercepts the linked list
js 将字符串作为js执行代码使用
[C Language] Getting Started
【FPGA】day22-SPI protocol loopback
[ADI low-power 2k code] Based on ADuCM4050, ADXL363, TMP75 acceleration, temperature detection and serial port printing, buzzer playing music (lone warrior)
电商项目——商城限时秒杀功能系统
Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
The thirteenth day of learning programming
【ADI低功耗2k代码】基于ADuCM4050的ADXL363、TMP75的加速度、温度检测及串口打印、蜂鸣器播放音乐(孤勇者)
LeetCode刷题第16天之《239滑动窗口最大值》
Leetcode 108. 将有序数组转换为二叉搜索树
Qnet Weak Network Test Tool Operation Guide
Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method
MySQL数据库存储引擎以及数据库的创建、修改与删除
机器学习中什么是集成学习?
【FPGA】abbreviation
2022-08-10 The sixth group Hiding spring study notes