当前位置:网站首页>"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及其验证
边栏推荐
- LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
- 【FPGA】abbreviation
- How to delete statements audit log?
- C# 一周入门高级编程之《C#-LINQ》Day Four
- I didn't expect MySQL to ask these...
- 大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
- 2022-08-10 The sixth group Hiding spring study notes
- 用户如何克服程序化交易中的情绪问题?
- Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
- uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
猜你喜欢

How does MSP430 download programs to the board?(IAR MSPFET CCS)

【FPGA】名词缩写

console.log alternatives you didn't know about

What should I do if the channel ServerID is incorrect when EasyCVR is connected to a Hikvision Dahua device and selects another cluster server?

EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?

树莓派入门(5)系统备份

大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?

LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》

es-head插件插入查询以及条件查询(五)

Power Cabinet Data Monitoring RTU
随机推荐
高度塌陷问题的解决办法
[FPGA] day19- binary to decimal (BCD code)
VIT 源码详解
机器学习是什么?详解机器学习概念
一次简单的 JVM 调优,学会拿去写到简历里
论文精度 —— 2017 CVPR《High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis》
你不知道的 console.log 替代品
电商项目——商城限时秒杀功能系统
Binary tree related code questions [more complete] C language
【FPGA】设计思路——I2C协议
typedef定义结构体数组类型
Power Cabinet Data Monitoring RTU
MYSQLg advanced ------ return table
Qnet Weak Network Test Tool Operation Guide
Kubernetes集群搭建Zabbix监控平台
多串口RS485工业网关BL110
【ADI低功耗2k代码】基于ADuCM4050的ADXL363、TMP75的加速度、温度检测及串口打印、蜂鸣器播放音乐(孤勇者)
pathman_config、pathman_config_params 删除后,如何重建?
Homework 8.10 TFTP protocol download function
leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》