当前位置: 首页 > news >正文

LeetCode98. Validate Binary Search Tree

文章目录

    • 一、题目
    • 二、题解

一、题目

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid BST is defined as follows:

The left
subtree
of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.

Example 1:

Input: root = [2,1,3]
Output: true
Example 2:

Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node’s value is 5 but its right child’s value is 4.

Constraints:

The number of nodes in the tree is in the range [1, 104].
-231 <= Node.val <= 231 - 1

二、题解

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> path;void inOrder(TreeNode* root){if(!root) return;inOrder(root->left);path.push_back(root->val);inOrder(root->right);}bool isValidBST(TreeNode* root) {inOrder(root);int n = path.size();for(int i = 0;i < n - 1;i++){if(path[i] >= path[i+1]) return false;}return true;}
};
http://www.lryc.cn/news/233677.html

相关文章:

  • 【LeetCode】206. 反转链表
  • 飞天使-通过GET 和POST进案例演示
  • 【MySql】12- 实践篇(十)
  • <C++> 反向迭代器
  • 【EI会议征稿】第三届网络安全、人工智能与数字经济国际学术会议(CSAIDE 2024)
  • 格力报案称“高管遭自媒体侮辱诽谤”
  • HBase之Compaction
  • 设计模式之结构型模式
  • centOs 6.10 编译 qt 5.15.11
  • Redis对象的数据结构及其原理汇总
  • @RestController 注解网页返回 [] ,出现的bug
  • C语言指针详解(1)(能看懂字就能明白系列)文章超长,慢慢品尝
  • 为什么别人年薪30W+?同样为测试人,“我“的测试之路...
  • 【Unity】XML文件的解析和生成
  • Vue h5页面手指滑动图片
  • Python类属性下划线的意义
  • DbUtils概述
  • 大数据基础设施搭建 - Hadoop
  • 测试开发环境下centos7.9下安装docker的minio
  • Django之模版层
  • spark性能调优 | 内存优化
  • 【PG】PostgreSQL高可用之自动故障转移-repmgrd
  • 操作系统OS/存储管理/内存管理/内存管理的主要功能_基本原理_要求
  • 【手写数据库toadb】SQL解析器的实现架构,create table/insert 多values语句的解析树生成流程和输出结构分析
  • 设计模式-备忘录模式-笔记
  • 机器学习—基本术语
  • pytorch单精度、半精度、混合精度、单卡、多卡(DP / DDP)、FSDP、DeepSpeed模型训练
  • 基于PHP的纺织用品商城系统
  • Go使用命令行输出二维码
  • 最长连续序列[中等]