当前位置:网站首页>10. < tag binary tree and BST foundation > lt.700 Search in binary search tree + lt.98 Validate binary search tree + lt.530 Minimum absolute difference of binary search tree (the same as lt.783)
10. < tag binary tree and BST foundation > lt.700 Search in binary search tree + lt.98 Validate binary search tree + lt.530 Minimum absolute difference of binary search tree (the same as lt.783)
2022-06-09 12:01:00 【Caicai's big data development path】
lt.700. Search in binary search tree
[ Case needs ]

[ Thought analysis ]
- This is a simple question worthy of the name
- BST: Binary search tree , For each node in the tree ,
- The value of her left node is less than this node ,
- The value of her right node is greater than this node ;

- The value of her right node is greater than this node ;
[ Code implementation one , Recursive method ]
class Solution {
//1. Recursive function , lookup BST, Return value found node
public TreeNode searchBST(TreeNode root, int val) {
//2. Recursion end condition
if(root == null || root.val == val)return root;
//3. Single layer recursive logic
// if(root.val == val){
// return root;
// }else
if(root.val > val){
return searchBST(root.left, val);
}else{
return searchBST(root.right, val);
}
}
}
Simplified edition
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) return root;
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}

[ Code implementation 2 , Iterative method ]

public TreeNode searchBST(TreeNode root, int val) {
while (root != null && root.val != val) {
root = root.val > val ? root.left : root.right;
}
return root;
}
lt.98. Verify binary search tree
[ Case needs ]

[ Train of thought analysis 1 , recursive ]

[ Code implementation ]
/** * 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; * } * } */
class Solution {
//1. Recursive function , Verify that each node is BST,
// Parameters : The current node that is considered the root node
// return : It's still not
//long maxVal = Integer.MIN_VALUE; ---> -long, great
TreeNode pre = null;
public boolean isValidBST(TreeNode root) {
//2. Recursive export
if(root == null)return true;
//3. Single layer recursive logic
// BST The middle order traversal in is an increasing sequence
boolean left = isValidBST(root.left); // The left subtree
if(pre != null && pre.val >= root.val){
return false;
}else{
pre = root;
}
boolean right = isValidBST(root.right); // Right subtree
return left && right;
}
}
[ Train of thought analysis II , aggregate + Recursive method ]
- Remember this sentence , For binary search tree (BST) Do middle order traversal , What you get is a sequence in ascending order
- So we can BST Do middle order traversal , While traversing, put the encountered number into the set ( Because the length is unknown , Unavailable array ), At the same time, compare whether the last number of the set and the number to be stored are in ascending order .
[ Code implementation ]
/** * 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; * } * } */
class Solution {
List<Integer> list = new ArrayList<>();
public boolean isValidBST(TreeNode root) {
//1. Put in collection
return dfs(root);
}
public boolean dfs(TreeNode root){
//
if(root == null)return true;
// Single layer recursive logic
boolean left = dfs(root.left);
// 1. Set is not empty , And to be deposited root.val > list Last element of , ascend , That's right
if(!list.isEmpty() && list.get(list.size() - 1) < root.val){
list.add(root.val);
// 2. Set is not empty , And to be deposited root.val <= list Last element of , Non ascending , This is not true
}else if(!list.isEmpty() && list.get(list.size() - 1) >= root.val){
return false;
}else{
//3. When the collection is empty , Store elements directly
list.add(root.val);
}
boolean right = dfs(root.right);
return left && right;
}
}

lt.530. The minimum absolute difference of binary search tree ( Same as lt.783)
[ Case needs ]

[ Train of thought analysis 1 , aggregate + recursive ]

[ Code implementation ]
class Solution {
List<Integer> list = new ArrayList<>();
int res = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) {
if(root == null)return 0;
getMinimumDifference(root.left);
if(list.size() > 0){
res = Math.min(res, root.val - list.get(list.size() - 1));
}
list.add(root.val);
getMinimumDifference(root.right);
return res;
}
}
[ Train of thought analysis II , recursive + pre Record the last traversal node ]
[ Code implementation ]
//1. recursive + Record the previous node
/** * 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; * } * } */
class Solution {
//1. Recursive function
int res = Integer.MAX_VALUE;
TreeNode pre = null;
public int getMinimumDifference(TreeNode root) {
//2. Recursive export
if(root == null)return 0;
//3. Single layer recursive logic ( In the sequence traversal )
getMinimumDifference(root.left);
if(pre != null){
res = Math.min(res, root.val - pre.val);
}
pre = root;
getMinimumDifference(root.right);
return res;
}
}
边栏推荐
- tag回溯-刷题预备知识-1. 回溯法模板, + lt.46. 全排列
- 工具类记录之Guawa的Splitter
- Iphone5s display disabled solution
- 8K resolution 7680*4320
- 如何通过鸿蒙生态赚钱?
- No remote desktop license server can provide licenses
- IPv6 address assignment
- OpenSSL security vulnerability (cve-2016-8610) repair steps
- U8g2 graphics library and STM32 migration (I2C, software and hardware)
- H3C certified cloud computing Engineer
猜你喜欢

使用U盘一比一拷贝核心板系统镜像的方法

03 | 中台定义:当我们谈中台时到底在谈些什么?

06 | 中台落地第一步:企业战略分解及现状调研(Discovery)

06 | the first step of China Taiwan landing: enterprise strategy decomposition and current situation research (Discovery)

05 | D4模型:中台规划建设方法论概述

Using kubekey to build kubernetes/kubesphere environment

No remote desktop license server can provide licenses

6. exchange the nodes in the linked list in pairs

03 | definition of China Taiwan: what are we talking about when we talk about China Taiwan?

06 | the first step of China Taiwan landing: enterprise strategy decomposition and current situation research (Discovery)
随机推荐
Win10 20h2 was officially released, and the list of old and new features was compared
Spark is too slow to write Doris solution
9.<tag-回溯和同层内去重的组合问题>lt.491. 递增子序列
流畅看1080p、2k、4k视频需要多大带宽?
VMware vSphere 6.5配置系列
H3C certified routing switching network senior engineer
R语言测试构建好的决策树模型(基于测试数据集)、进行预测推理并使用table函数计算混淆矩阵、基于混淆矩阵计算模型准确度accuray
How to uninstall IE browser in win7 system
09 | the fourth step: the construction and delivery of the middle office
使用U盘一比一拷贝核心板系统镜像的方法
Configurationmanager pose flash
中国科学院院刊 | 包云岗:加速发展关键核心技术,必须把握技术发展的自身规律
What is the difference between a fire engineer and a fireman?
06 | 中台落地第一步:企业战略分解及现状调研(Discovery)
OpenSSL security vulnerability (cve-2016-8610) repair steps
8K分辨率7680*4320
请你说说乐观锁和悲观锁,以及适用场景
Preparation guide for the 2022 soft exam network engineer exam
Map的遍历几种方式
H3C认证云计算工程师