当前位置:网站首页>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);
}
}
边栏推荐
- Cryptography series: detailed explanation of online certificate status protocol OCSP
- GCC compilation error
- Routing strategy of multi-point republication [Huawei]
- 【从 0 开始学微服务】【00】课程概述
- 2022 practice questions and mock examination of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge)
- HZOJ #235. 递归实现指数型枚举
- Simple implementation of call, bind and apply
- Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
- Talk about four cluster schemes of redis cache, and their advantages and disadvantages
- How to use PS link layer and shortcut keys, and how to do PS layer link
猜你喜欢
Common knowledge of one-dimensional array and two-dimensional array
图像像素读写操作
2022 polymerization process test question simulation test question bank and online simulation test
On valuation model (II): PE index II - PE band
How to apply @transactional transaction annotation to perfection?
How to use PS link layer and shortcut keys, and how to do PS layer link
[pytorch practice] use pytorch to realize image style migration based on neural network
解密GD32 MCU产品家族,开发板该怎么选?
Static comprehensive experiment
浅谈估值模型 (二): PE指标II——PE Band
随机推荐
Image pixel read / write operation
Day-18 hash table, generic
Simple implementation of call, bind and apply
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
HZOJ #240. 图形打印四
Cryptography series: detailed explanation of online certificate status protocol OCSP
通讯协议设计与实现
Visual stdio 2017 about the environment configuration of opencv4.1
如何将 @Transactional 事务注解运用到炉火纯青?
Day-17 connection set
3D content generation based on nerf
Attack and defense world ----- summary of web knowledge points
图形对象的创建与赋值
[statistical learning method] learning notes - support vector machine (I)
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
opencv的四个函数
[statistical learning method] learning notes - support vector machine (Part 2)
图像像素读写操作
[learn microservice from 0] [01] what is microservice
[learn wechat from 0] [00] Course Overview