当前位置:网站首页>Force deduction brush question - 98 Validate binary search tree
Force deduction brush question - 98 Validate binary search tree
2022-07-06 20:17:00 【Java mengxinling】
Daily clock in algorithm problem
subject : Give you the root node of a binary tree root , Determine whether it is an effective binary search tree .
A valid binary search tree is defined as follows :
1. The left subtree of the node contains only Less than Number of current nodes .
2. The right subtree of the node contains only Greater than Number of current nodes .
3. All left and right subtrees themselves must also be binary search trees .


The class definition of the following binary tree is given
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/Their thinking :
First, let's analyze the problem , The subject is very simple , It is to judge whether a given binary tree is a binary search tree .
Binary search tree :
1. For the binary tree, the maximum value of the left subtree of any node is less than the value of the node
2. For any node of the binary tree, the minimum value of the right subtree is greater than the value of the node
3. The left and right subtrees of any node of the binary tree are binary search trees
Set upper and lower bounds :
Every time I recurse to the left , Keep the original minimum value unchanged , Set the value of the current intermediate node to the upper bound
Every time I recurse to the right , Keep the original maximum value unchanged , Set the value of the current intermediate node to the lower bound
Set recursive exit :
Obviously , If the value of the current node does not meet the upper and lower bounds , Then the subtree is not a binary search tree , The root node is not satisfied .
public boolean isValidBST(TreeNode root){
return isValidBST(root,10000000000L,-10000000000L);
}
public boolean isValidBST(TreeNode root,long top,long low) {
if (root.val <= low || root.val >= top){
return false;
}
boolean left = false;
boolean right = false;
if(root.left != null){
left = isValidBST(root.left,root.val,low);
}else {
left = true;
}
if (root.right != null){
right = isValidBST(root.right,top, root.val);
}else {
right = true;
}
if (left && right){
return true;
}else {
return false;
}
}Pruning optimization :
The code verifies that the binary sort tree must pass through all nodes , If the binary tree is a sort binary tree , Then there is no doubt that all nodes will be verified , But if the tree is not a sorted binary tree , Other recursive paths will continue to be verified , So you can set a global variable outside the method boolean flag = true; Indicates that there are no nodes that are not satisfied , When dissatisfaction occurs, it is set to false, At this time, the initial judgment of the entry method flag, If false Cancel code running directly .
boolean flag = true;
public boolean isValidBST(TreeNode root){
return isValidBST(root,10000000000L,-10000000000L);
}
public boolean isValidBST(TreeNode root,long top,long low) {
if(!flag){
return false;
}
if (root.val <= low || root.val >= top){
return false;
}
boolean left = false;
boolean right = false;
if(root.left != null){
left = isValidBST(root.left,root.val,low);
}else {
left = true;
}
if (root.right != null){
right = isValidBST(root.right,top, root.val);
}else {
right = true;
}
if (left && right){
return true;
}else {
return false;
}
}边栏推荐
- js获取浏览器系统语言
- Tencent T2 Daniel explained in person and doubled his job hopping salary
- Zoom with unity mouse wheel: zoom the camera closer or farther
- Method keywords deprecated, externalprocname, final, forcegenerate
- How to select several hard coded SQL rows- How to select several hardcoded SQL rows?
- Tencent cloud database public cloud market ranks top 2!
- In unity space, an object moves around a fixed point on the sphere at a fixed speed
- 5. 無線體內納米網:十大“可行嗎?”問題
- 夏志刚介绍
- POJ3617 Best Cow Line 馋
猜你喜欢

Tencent T3 Daniel will teach you hand-in-hand, the internal information of the factory

Error analysis ~csdn rebound shell error

【GET-4】
![[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN](/img/b8/3d48e185bb6eafcdd49889f0a90657.png)
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
![[network planning] Chapter 3 data link layer (3) channel division medium access control](/img/df/dd84508dfa2449c31d72c808c50dc0.png)
[network planning] Chapter 3 data link layer (3) channel division medium access control
Tencent T2 Daniel explained in person and doubled his job hopping salary

rt-thread i2c 使用教程

持续测试(CT)实战经验分享

JMeter server resource indicator monitoring (CPU, memory, etc.)

Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing
随机推荐
Web security - payload
Transformer model (pytorch code explanation)
【GET-4】
JVM_ Common [interview questions]
Initial experience of addresssanitizer Technology
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
Logstash expressway entrance
POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
Unity load AB package
【云原生与5G】微服务加持5G核心网
Ideas and methods of system and application monitoring
2022年6月语音合成(TTS)和语音识别(ASR)论文月报
Cesium Click to draw a circle (dynamically draw a circle)
AsyncHandler
Tencent architects first, 2022 Android interview written examination summary
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
Method keywords deprecated, externalprocname, final, forcegenerate
Groovy basic syntax collation
golang的超时处理使用技巧
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother