当前位置:网站首页>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);
}
}
边栏推荐
- [learn microservices from 0] [03] explore the microservice architecture
- leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
- BGP actual network configuration
- xshell评估期已过怎么办
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- 密码学系列之:在线证书状态协议OCSP详解
- 编译 libssl 报错
- Static vxlan configuration
- 【从 0 开始学微服务】【02】从单体应用走向服务化
- [crawler] avoid script detection when using selenium
猜你喜欢
JS to convert array to tree data
How to apply @transactional transaction annotation to perfection?
[statistical learning methods] learning notes - Chapter 5: Decision Tree
【统计学习方法】学习笔记——提升方法
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
解密GD32 MCU产品家族,开发板该怎么选?
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
leetcode刷题:二叉树24(二叉树的最近公共祖先)
Day-14 common APIs
What if does not match your user account appears when submitting the code?
随机推荐
Creation and assignment of graphic objects
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
AirServer自动接收多画面投屏或者跨设备投屏
[learn micro services from 0] [02] move from single application to service
Aike AI frontier promotion (7.7)
opencv的四个函数
SQL Lab (46~53) (continuous update later) order by injection
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
Decrypt gd32 MCU product family, how to choose the development board?
What is an esp/msr partition and how to create an esp/msr partition
Polymorphism, final, etc
Four functions of opencv
广州市召开安全生产工作会议
Realize all, race, allsettled and any of the simple version of promise by yourself
2022聚合工艺考试题模拟考试题库及在线模拟考试
@Resource和@Autowired的区别?
编译 libssl 报错
【从 0 开始学微服务】【00】课程概述
Airserver automatically receives multi screen projection or cross device projection
[learn wechat from 0] [00] Course Overview