题目描述

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. hdu6468(记忆化搜索)

    zyb的面试 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  2. 编程微语 2019-Autumn

    很多时候我们要的是[网页全屏],可是许多软件却做成了[浏览器全屏],不要一听到[全屏]就认为真的是传统意义上的全屏.拜托,老板(往往就是最大的产品经理).产品经理.程序员,想想,说清楚,做正确.某度文 ...

  3. Markdown 小记

    在学习Markdown之前,对稍有轻微强迫症的我来说,写博客和做笔记是一件很痛苦的事.废话不多说直接来看,偷偷吐槽:不知道咋在博客园配置Markdown,以后如果学会了回来补充. 标题 #hello一 ...

  4. linux内核中#if IS_ENABLED(CONFIG_XXX)与#ifdef CONFIG_XXX的区别

    1. #if IS_ENABLED(CONFIG_XXX) 1.1 IS_ENABLED的定义如下: /* * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CON ...

  5. mac kafka 环境搭建 以及PHP的kafka扩展

    1.kafka安装 brew install kafka 安装会依赖zookeeper. 注意:安装目录:/usr/local/Cellar/kafka/0.10.2.0 2.安装的配置文件位置 /u ...

  6. IO流概述

    作用: 处理设备之间的数据传输的 I: Input(输入) , O: Output(输出) 什么是输入和输出? 我们说输入和输出问题是站在内存的角度而言 , 如果我们程序读取硬盘上的数据那么就是输入 ...

  7. jpeglib.h jerror.h No such file or directory 以及 SDL/SDL.h: 没有那个文件

    1. error: jpeglib.h jerror.h No such file or directory 没有那个文件或目录 jpeg.cc:19:21:error: jpeglib.h: 没有那 ...

  8. Unicode浅析——调用科大讯飞语音合成接口(日语)所遇到的天坑

    如题,最近做的项目需要调用科大讯飞的语音合成接口,将日文合成日语.然后坑爹的是跟我对接的那一方直接扔过来一份接口文档,里面并未提及日语合成所需要的参数.中文.英文合成倒是没问题,就这个日语合成的音频始 ...

  9. 关于mysql索引---联合索引

    结论: mysql联合索引,联合索引以哪个字段开始很重要. 如果  联合索引字段为  1,2,3,4 那么如果查询条件为  6,7,8,1 这样也会走上面的联合索引 但是如果查询条件不是从1开始那么则 ...

  10. 微信支付 composer 方法 --- 实测有效

    <h1 align="center">Pay</h1> <p align="center"> <a href=&quo ...