当前位置:网站首页>Leetcode skimming: binary tree 21 (verifying binary search tree)
Leetcode skimming: binary tree 21 (verifying binary search tree)
2022-07-07 12:48:00 【Taotao can't learn English】
98. Verify binary search tree
Given a binary tree , Determine whether it is an effective binary search tree .
Suppose a binary search tree has the following characteristics :
- The left subtree of a node contains only the number of nodes smaller than the current node .
- The right subtree of a node contains only the number of nodes greater than the current node .
- All left and right subtrees themselves must also be binary search trees .

Traverse the middle order of the binary tree , Then traverse the result set , If the result set is in ascending order , It is a binary search tree , Because the nature of binary search tree is The middle order traversal is non decreasing . Tips for this question Ask for incremental , So there will be no equality .
package com.programmercarl.tree;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
/** * @ClassName IsValidBST * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 21:32 * @Version 1.0 * https://leetcode.cn/problems/validate-binary-search-tree/ * 98. Verify binary search tree **/
public class IsValidBST {
/** * Direct middle order traversal to obtain the result * If the result is Increasing , Then the verification is successful * * @param root * @return */
public boolean isValidBST(TreeNode root) {
if (root.left == null && root.right == null) {
return true;
}
Deque<Integer> deque = new ArrayDeque<Integer>();
inorderTraversal(root, deque);
Integer pre = deque.poll();
Integer last = null;
// Compare the previous value with the latter value , If not in ascending order , Is not a binary search tree
while (!deque.isEmpty()) {
last = deque.poll();
if (pre >= last) {
return false;
} else {
pre = last;
}
}
return true;
}
public void inorderTraversal(TreeNode root, Deque<Integer> deque) {
if (root == null) {
return;
}
// In the sequence traversal Left in Right
inorderTraversal(root.left, deque);
deque.offer(root.val);
inorderTraversal(root.right, deque);
}
}

边栏推荐
- 2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
- 3D content generation based on nerf
- Realize all, race, allsettled and any of the simple version of promise by yourself
- OSPF exercise Report
- Simple implementation of call, bind and apply
- [疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
- Object. Simple implementation of assign()
- [statistical learning methods] learning notes - Chapter 4: naive Bayesian method
- SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
- Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
猜你喜欢

Static routing assignment of network reachable and telent connections

leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)

leetcode刷题:二叉树20(二叉搜索树中的搜索)

BGP third experiment report

Vxlan 静态集中网关
![[pytorch practice] write poetry with RNN](/img/91/a6d3f348ff099b7c44eb185921b1b6.png)
[pytorch practice] write poetry with RNN

What is an esp/msr partition and how to create an esp/msr partition

Charles: four ways to modify the input parameters or return results of the interface

Aike AI frontier promotion (7.7)

About sqli lab less-15 using or instead of and parsing
随机推荐
leetcode刷题:二叉树24(二叉树的最近公共祖先)
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
[statistical learning methods] learning notes - Chapter 5: Decision Tree
有什么类方法或是函数可以查看某个项目的Laravel版本的?
@Resource和@Autowired的区别?
SQL head injection -- injection principle and essence
[learn wechat from 0] [00] Course Overview
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
2022-07-07日报:GAN发明者Ian Goodfellow正式加入DeepMind
mysql怎么创建,删除,查看索引?
Object. Simple implementation of assign()
【PyTorch实战】用RNN写诗
Aike AI frontier promotion (7.7)
Day-17 connection set
"Series after reading" my God! It's so simple to understand throttling and anti shake~
HZOJ #235. 递归实现指数型枚举
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
利用棧來實現二進制轉化為十進制