给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:

  • 节点的左子树只包含小于当前节点的数。
  • 节点的右子树只包含大于当前节点的数。
  • 所有左子树和右子树自身必须也是二叉搜索树。

示例 1:

输入: 2 / \ 1 3 输出: true

示例 2:

输入: 5 / \ 1 4   / \   3 6 输出: false 解释: 输入为: [5,1,4,null,null,3,6]。   根节点的值为 5 ,但是其右子节点值为 4 。

class Solution {
public:
bool isValidBST(TreeNode* root)
{
if(root == NULL)
return true;
if(!CheckLeft(root ->val, root ->left))
return false;
if(!CheckRight(root ->val, root ->right))
return false;
return isValidBST(root ->left) && isValidBST(root ->right);
} bool CheckLeft(int val, TreeNode* root)
{
if(root == NULL)
return true;
if(root ->val >= val)
return false;
return CheckLeft(val, root ->left) && CheckLeft(val, root ->right);
} bool CheckRight(int val, TreeNode* root)
{
if(root == NULL)
return true;
if(root ->val <= val)
return false;
return CheckRight(val, root ->left) && CheckRight(val, root ->right);
}
};

Leetcode98. Validate Binary Search Tree验证二叉搜索树的更多相关文章

  1. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

  2. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  4. 098 Validate Binary Search Tree 验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义:    左子树只包含小于当前节点的数.    右子树只包含大于当前节点的数.    所有子树自身必须也是二叉搜索树.示例 1 ...

  5. [leetcode]98. Validate Binary Search Tree验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  7. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树

    判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...

  9. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. <Django> 第三方扩展

    1.富文本编辑器 tinymce为例 安装 pip install django-tinymce 在settings.py中的配置 配置应用 INSTALLED_APPS = [ 'django.co ...

  2. java关于lombok(包括父类参数)

    java关于lombok对bean对象进行自动设置 使用说明 使用方式 注释类型 @NonNull @Data(常用) @NoArgsConstructor(常用)/@RequiredArgsCons ...

  3. 2、docker镜像管理

    Docker镜像管理 镜像是Docker容器的基础,想运行一个Docker容器就需要有镜像.我们上面已经学会了使用search搜索镜像.那么这个镜像是怎么创建的呢? 创建镜像 镜像的创建有以下几种方法 ...

  4. 编译安装redis-3.2.9(latest stable version)

    What is the Redis? Redis is an open source (BSD licensed), in-memory data structure store, used as a ...

  5. 我王某的 低错&&(女装)Flag

    日月共鉴,人心同睹. 喜欢你. 喜欢你的眼睛. 春天银河般闪烁的双瞳.春日阳光般温柔的眼神. 喜欢你的头发. 在微风中飘拂丝绢般柔滑的长发. 喜欢你的嘴唇. 给我甜蜜的吻.吐出忧伤叹息的嘴唇. 喜欢你 ...

  6. iOS7新特性-完美解决iOS7关于自定义导航条UIBarButtonItem偏移的问题

    前言: 本文由DevDiv社区@Vincent 原创,转载请注明出处! http://www.devdiv.com/iOS_iPhone-ios_ios_uibarbuttonitem_-thread ...

  7. JDK源码阅读--Object

    在java.lang包下 Object类:是所有类的基类(父类) public final native Class<?> getClass(); 返回这个Object所代表的的运行时类 ...

  8. Linux 启动、关闭、重启服务的两种方式

    1.一种是可以使用service脚本来调度,如: service 服务名 startservice 服务名 stopservice 服务名 restart 2.第二种可以直接进入/etc/init.d ...

  9. Struts2OGNL

    OGNL: 什么是OGNL  Object Graph Navigation Language 开源项目,取代页面中Java脚本,简化数据访问 和EL同属于表达式语言,但功能更为强大  OGNL在St ...

  10. Python学习day25-面向对象之组合,多态和封装

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...