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.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

判断一棵二叉树是否是二叉搜索树,我们可以采用中序遍历来看二叉树的中序遍历是否是递增的,如果是递增的,那么就是BST,如果不是递增的,那么就不是BST。

代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public long n = (long)Integer.MIN_VALUE-1;
public boolean isValidBST(TreeNode root) {
if(root == null) return true;
boolean a = isValidBST(root.left);
if(root.val<=n) return false;
else n = root.val;
boolean b = isValidBST(root.right);
return (a&&b);
}
}

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

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

  4. LeetCode OJ: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. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

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

  7. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  8. 【LeetCode练习题】Validate Binary Search Tree

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

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

随机推荐

  1. SEO定义目的,优化的好处

    SEO:search engine optimization(搜索引擎优化) SEO严谨的定义:SEO是指在了解搜索引擎自然排名机制的基础上,对网站进行内部及外部的调整优化,改进网站在搜索引擎中关键字 ...

  2. CocoaPods 报错 [!] The dependency `JSONModel (~> 1.2.0)` is not used in any concrete target.

    当用CocoaPods  pod install 时出现了下面的错误时: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; col ...

  3. 【MySQL】filesort.cc 源代码阅读笔记

    最近阅读了部分MySQL排序的代码,把心得记录一下. 参考代码 MySQL: 文件: filesort.cc 函数: filesort() 排序过程伪代码 function filesort(tabl ...

  4. Maze

    Maze 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5094 BFS+状态压缩 把身上携带钥匙的状态压缩成一个2^10的整数.这道题难在 ...

  5. 在家用机上搭建 Git https 服务器

    今天主要叙述在家里台式机的虚拟机上搭建支持 https 的 ubuntu git 服务器. 实际上,从一个用户请求家里 git 服务器代码,最终完成代码的传输,主要是通过以下的过程: 首先,从外界寻找 ...

  6. js-学习方法之3

    熟悉JavaScript每一个方法的作用 这一要求听起来似乎有点不太实际,我想这个要求对于像C#.JAVA这些大型语言来说确实是,因为这些语言类库实在太庞大了,相信没有人可以全面记住它,而且也是没有必 ...

  7. ubuntu下pip install mysql-python 失败的解决方案

    ubuntu连接mysql 需要安装mysql-python 出现can not find mysql-config 文件错误 先安装 sudo apt-get install libmysqld-d ...

  8. OSCache 使用

    引入OSCache的jar包 package com.sun.utils; import java.util.Date; import com.opensymphony.oscache.base.Ne ...

  9. POJ 1922 Ride to School#贪心

    (- ̄▽ ̄)-* //C跟着a君骑,然后更快的b君来了,C又跟着b君骑, //接着最快的d君来了,C就去跟着d君了, //最后最快的d君到达目的地时,C也就到了 //所以C的到达时间,就是最早到达的那 ...

  10. deepin2014.1快捷键

    初试deepin2014.1,发现windows很多快捷键在deepin中也完美支持,举例如下: ctrl+shift+n : 新建文件夹 窗口键+E:打开文件系统 窗口键+TAB:3D切换桌面 al ...