题目描述

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 Input: [2,1,3]
Output: true

Example 2:

    5
/ \
1 4
  / \
  3 6 Input: [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.

参考答案

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

答案分析

LC 98. Validate Binary Search Tree的更多相关文章

  1. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  2. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  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. 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 ...

  5. 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 98 Validate Binary Search Tree ----- java

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

  7. LeetCode OJ 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 ...

  8. 【LeetCode】98. Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  9. 【一天一道LeetCode】#98. Validate Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. YII框架的类自动加载机制

    YII之所以能实现快速的自动加载类文件,是因为它通过两种途径来实现. 先看文件 vendor\yiisoft\yii2\BaseYii.php 里面的 autoload 方法 public stati ...

  2. Linux的MySQL安装和配置(详细)

    打开centos系统 输入root用户和密码(我的用户和密码都是root) 查看有没有安装mysql rpm -qa|grep mysql 没有返回任何信息说明没有安装 我是用的centos7,默认安 ...

  3. Seata 中类SPI使用机制分析

    Seata中采用了与sofa-rpc和dubbo中相同的服务扩展机制.都是基于JAVA自身的服务发现机制-SPI进行再次封装注解,sofa-rpc和dubbo(@Deprecated)中的注解名字叫做 ...

  4. decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)

    decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下: IF 条件=值1 THEN RETURN(翻译值1) ELSIF 条件=值2 THEN RETU ...

  5. 2018-2019-2 网络对抗技术 20165231 Exp7 网络欺诈防范

    实践内容(3.5分) 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法.具体实践有 (1)简单应用SET工具建立冒名网站 (1分) (2)ettercap DNS spoof ...

  6. uploadify HTTP 302 错误如何解决?

    TP框架uploadify HTTP 302 错误如何解决?   在核心类文件夹里下的Conf/convention.php中 将 VAR_SESSION_ID打开(建议在模块的conf文件中添加配置 ...

  7. Java 中清空map

    本文链接:https://blog.csdn.net/TsuiXh/article/details/87879004在开发中在使用Map时,如果需要将Map作为临时的数据存储和处理,可以不用每次都去新 ...

  8. 临界区代码 critical section Locks and critical sections in multiple threads

    临界区 在同步的程序设计中,临界区段(Critical section)指的是一个访问共享资源(例如:共享设备或是共享存储器)的程序片段,而这些共享资源有无法同时被多个线程访问的特性. 当有线程进入临 ...

  9. keras Model 1 入门篇

    1 入门 2 多个输入和输出 3 共享层 最近在学习keras,它有一些实现好的特征提取的模型:resNet.vgg.而且是带权重的.用来做特诊提取比较方便 首先要知道keras有两种定义模型的方式: ...

  10. [Java复习] 分布式事务 Part 1

    1. CAP理论 C: Consistency 一致性 A: Availability 可用性 P: Partition tolerance 分区容错性 CAP定理:一个分布式系统不可能同时满足CAP ...