当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
BeagleBoneBlack 上手记
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
BUUCTF---Reverse---easyre
Example of shutter text component
5. Wireless in vivo nano network: top ten "feasible?" problem
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
Discussion on beegfs high availability mode
Standardized QCI characteristics
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
【计网】第三章 数据链路层(3)信道划分介质访问控制
随机推荐
Continuous test (CT) practical experience sharing
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
Unity writes a timer tool to start timing from the whole point. The format is: 00:00:00
Guangzhou's first data security summit will open in Baiyun District
logstash高速入口
PowerPivot——DAX(初识)
Groovy basic syntax collation
5. 無線體內納米網:十大“可行嗎?”問題
PowerPivot - DAX (first time)
Oceanbase Community Edition OBD mode deployment mode stand-alone installation
An East SMS login resurrection installation and deployment tutorial
02 基础入门-数据包拓展
Leetcode question 448 Find all missing numbers in the array
技术分享 | 抓包分析 TCP 协议
Synchronization of data create trigger synchronization table for each site
B-jiege's tree (pressed tree DP)
OceanBase社区版之OBD方式部署方式单机安装
【GET-4】
Standardized QCI characteristics
新一代垃圾回收器—ZGC