当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
Multi row and multi column flex layout
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
HZOJ #240. 图形打印四
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
Zhimei creative website exercise
Day-14 common APIs
Preorder, inorder and postorder traversal of binary tree
visual stdio 2017关于opencv4.1的环境配置
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
随机推荐
Using stack to convert binary to decimal
SQL head injection -- injection principle and essence
[learn wechat from 0] [00] Course Overview
Connect to blog method, overload, recursion
[pytorch practice] use pytorch to realize image style migration based on neural network
Airserver automatically receives multi screen projection or cross device projection
【统计学习方法】学习笔记——支持向量机(上)
Cryptography series: detailed explanation of online certificate status protocol OCSP
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
"Series after reading" my God! It's so simple to understand throttling and anti shake~
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
【PyTorch实战】图像描述——让神经网络看图讲故事
密码学系列之:在线证书状态协议OCSP详解
Guangzhou held work safety conference
Vxlan static centralized gateway
【深度学习】图像多标签分类任务,百度PaddleClas
SQL Lab (41~45) (continuous update later)
About web content security policy directive some test cases specified through meta elements
【统计学习方法】学习笔记——提升方法
SQL blind injection (WEB penetration)