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 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的更多相关文章
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【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) ...
- 【一天一道LeetCode】#98. Validate Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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 ...
- 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...
- 【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 ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [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 ...
随机推荐
- 找回误删除的UBUNTU16.04桌面壁纸图片,或把桌面背景图片另存。20170114
今天遇到一个小问题,之前下载并设置为桌面壁纸的一张图片在整理文件的时候不小心删除了.由于想不起来当时从哪里找到的图,所以就想把当前桌面壁纸重新保存.经网上查询,未见正确的保存方法,故写在此处备忘. 1 ...
- C1FlexGrid小结(转自http://www.cnblogs.com/C1SupportTeam/archive/2012/12/11/2812316.html)
C1FlexGrid控件来对一个表格格式中的数据进行显示,编辑,组和总结.该表格可以绑定到一个数据源,它可以对自己的数据进行管理. C1FlexGrid控件有一个包含以下元素的丰富的对象模型: 以下的 ...
- iOS开发工具箱
工欲善其事 必先利其器 最新重新安装了MacBook Air和Mac Pro的系统,彻底解决了一编译就报硬盘空间不足的问题.平时开发中比较常用的工具都需一一安装. XCode:没什么好说的,外星人都知 ...
- 求指定范围里的不重复的N个随机数
原本是朋友问了一个题目,怎样把1到25个整形数随机排列,想了想,换个意思就是说如何把25个数随机不重复显示出来,即求1—25中25个随机数的一个数组.最简单的方法即利用双循环,是在每次得到一个随机数后 ...
- 神器XPath在Python下的使用
1.在python下使用xpath,需安装第三方库lxml 2.安装后,导入from lxml import etree selector=etree.HTML(html) Selector.xpat ...
- 第七十一,CSS颜色与度量单位
CSS颜色与度量单位 学习要点: 1.颜色表方案 2.度量单位 本章主要探讨HTML5中CSS颜色和度量单位等问题,包括颜色的选取方式.相对长度和绝对长度等. 一.颜色表方案 1 颜色的表现形式主 ...
- Nodejs(待补充)
Nodejs从入门到精通(待补充) 首先安装n模块: npm install -g n 第二步: 升级node.js到最新稳定版 n stable 是不是很简单?! n后面也可以跟随版本号比如: n ...
- Talking Ben App砸壳记
需求: 导出Talking Ben app的头文件 实施: 1)准备材料: 越狱IOS设备一部,并安装Talking Ben游戏 IOS设备上安装open SSH IOS设备的/usr/bin 中安装 ...
- 朱丽叶—Cuda+OSG
#include <cuda_runtime.h> #include <osg/Image> ; typedef struct cuComplex { float r; flo ...
- 【笔记】虚拟机用Xshell登陆报错“ssh服务器拒绝了密码”解决方法
自己本地安装了VMware Workstation之后,创建虚拟机,一开始是可以用Xshell登陆的,几天后,重启了电脑以及虚拟机,发现无法登陆了,还没有输入密码就有如下报错信息 这时候开始排查原因, ...