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

Assume a 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:

        2
    / \
    1 3

    Binary tree [2,1,3], return true.

    Example 2:

        1
    / \
    2 3

    Binary tree [1,2,3], return false.

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
return isValidBST(root, LONG_MIN, LONG_MAX);
}
bool isValidBST(TreeNode *root, long mn, long mx) {
if (!root) return true;
if (root->val <= mn || root->val >= mx) return false;
return isValidBST(root->left, mn, root->val) && isValidBST(root->right, root->val, mx);
}
};

Leecode_98_Validate_Binary_Search_Tree的更多相关文章

随机推荐

  1. CSPS模拟 45 乔迁之喜

    搬家了qwq 暑假在机房藏的吃的还没来得及吃qwq 有缘人会发现它的(其实并没有) 我居然也能和skyh并列了啊sto T1 安排打怪 不可能让左边的人越过右边人打的怪去打更靠右的怪吧? $O(n^2 ...

  2. VirtualBox6安装CentOS7设置静态IP

    安装virtualbox后安装centos7, 这里就不在赘述了, 网上有很多教程 先关闭虚拟机, 按照如下设置配置网络 这里需要使用双网卡, 我们在开启第二个网卡, 如下所示 之后开启虚拟机, 进行 ...

  3. php 全文搜索搜索-讯搜使用

    相信很多朋友遇到过,需要全文搜索的场景,百度了一圈发现了一个xunsearch 首先本地采集了1万篇文章,发现效率还可以. 使用上也很简单,直接上代码 //接收关键词 $xs = new XS('xp ...

  4. html5自动横屏的方法

    html5自动横屏的方法<pre>var evt = "onorientationchange" in window ? "orientationchange ...

  5. 关于GDAL读写Shp乱码的问题总结

    目录 1. 正文 1.1. shp文件本身的编码的问题 1.2. 设置读取的编码方式 1.2.1. GDAL设置 1.2.2. 解码方式 1.2.3. 其他 2. 参考 1. 正文 最近在使用GDAL ...

  6. U盘安装centos7 系统卡在 starting dracut initqueue hook

    U盘安装centos7启动过程中出现: [ok] Reached target Basic System 或者 [ok] starting dracut initqueue hook 下面是我解决的过 ...

  7. Material for oauth 2

    oauth 2 in 8 steps:  https://knpuniversity.com/screencast/oauth Live demo of oauth 2 (with server im ...

  8. [javascript] 编写一个计算器,实现加减法

    1.代码 <script> function sum(){ //加法 var value1 = document.getElementById("num1").valu ...

  9. lqb 基础练习 闰年判断

    基础练习 闰年判断 时间限制:1.0s   内存限制:256.0MB     问题描述 给定一个年份,判断这一年是不是闰年. 当以下情况之一满足时,这一年是闰年: 1. 年份是4的倍数而不是100的倍 ...

  10. windwos 10 安装flask

    1 安装python2.7.13 安装文件为:python-2.7.13.amd64.msi,因为python2.7.13中已经包含了pip. 在安装过程中选中[Add python.exe to P ...