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

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

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

示例 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. MyBatis的核心API

    MyBatis核心Api 上次简单的写了一个MyBatis的简介以及编写了一个MyBatis的入门程序,但是在入门程序中出现多很多比较陌生的词,比如SqlSessionFactoryBuilder.S ...

  2. Spring 声明式事务管理(11)

    案例分析 本案例是图书管理系统精简部分,在数据库中有3张表.分别保存图书库存.图书信息和用户信息.下面是建表SQL语句 1 DROP TABLE IF EXISTS store; 2 DROP TAB ...

  3. System.UriFormatException: Invalid URI 解决方法

    mobox 企业网页登陆界面,sa 登陆后 提示 System.UriFormatException: Invalid URI: The URI scheme is not valid. at Sys ...

  4. Python基础笔记_变量类型

     下面是W3C学习笔记 , , ) :] ]) :]) :]) :-]) :-]) ]) :]) :]) ) , , ]) :]) :]) ) , , , ]) :]) :]) ) ] = , ])) ...

  5. hdu 5382

    \(F(n)=\sum_{i=1}^n\sum_{j=1}^n[lcm(i,j)+gcd(i,j)\geq n]\) \(S(n)=\sum_{i=1}^nF(i)\) \(F(n)=n^2-\sum ...

  6. HZOI20190725 B 回家 tarjan

    题目大意:https://www.cnblogs.com/Juve/articles/11226266.html 题解: 感觉挺水的,但考场上没打出来 题目翻译一下就是输出起点到终点必经的点 其实就是 ...

  7. https,http和ssl的关系

    https,http和ssl这三者是什么关系呢? http是超文本传输协议,浏览器利用这一协议就可以访问各种网站了. ssl指Secure Sockets Layer 安全套接层,是一种保证网络数据传 ...

  8. Error: Could not link: /usr/local/share/doc/homebrew

    mac 执行brew update 报错 Error: Could not link: /usr/local/share/doc/homebrew 更新brew,报错 Error: Could not ...

  9. 将wordpress中的文章导出为markdown

    一.进入wordpress后台,选择工具-导出数据,选择你需要导出的内容.文章等,会下载一个xml文件到本地电脑 二.使用一个名为wordpress-to-markdown的工具 源码地址:wordp ...

  10. 打开springboot的run dashboard

    默认情况下,idea的run dashboard是关闭的,当检测到你有多个springboot项目时会弹出提示框,询问是否打开. 如果我们想要自己打开,需要修改配置. 在你的idea的项目目录中,有一 ...