LintCode Validate Binary Search Tree
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 thanthe node's key.
- Both the left and right subtrees must also be binary search trees.
- A single node tree is a BST
Example
An example:
2
/ \
1 4
/ \
3 5
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if the binary tree is BST, or false
*/
public boolean isValidBST(TreeNode root) {
// write your code here
if (root == null) {
return true;
}
if ((root.left == null) && (root.right == null)) {
return true;
} boolean left = false;
boolean right = false;
if (root.left == null) {
left = true;
}
else if ((root.left != null) && (root.left.val < root.val)) {
left = isValidBST(root.left);
}else {
return false;
} if (root.right == null) {
right = true;
}
else if ((root.right != null) && (root.right.val > root.val)) {
right = isValidBST(root.right);
}else {
return false;
} return left && right;
}
}
LintCode Validate Binary Search Tree的更多相关文章
- CCI4.5/LintCode Validate Binary Search Tree
Validate BST是指按中序遍历niorder后左<node<右: 第一种方法: 先按inoreder遍历, 再放进ArrayList里用循环看是不是从小到大排序: 注意: 设置成员 ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- Arduino学习笔记二:修改LED点灯程序
看了开源社区的LED控制程序,开始上手代码编写,修改,下载以及调试,原文地址:http://www.arduino.cn/thread-1072-1-1.html,这个帖子写的比较通俗易懂. 自己移植 ...
- 教你解决Sublime Text中文乱码问题
教你解决Sublime Text中文乱码问题[转载自成长的企鹅] Sublime Text 2是一个非常不错的源代码及文本编辑器,但是不支持GB2312和GBK编码在很多情况下会非常麻烦. ...
- oracle数据库的TNS配置
TNS简要介绍与应用 Oracle中TNS的完整定义:transparence Network Substrate透明网络底层,监听服务是它重要的一部分,不是全部,不要把TNS当作只是监听器. TNS ...
- HDU 5832 A water problem(某水题)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- java练习题(字符串类):显示4位验证码、输出年月日、从XML中抓取信息
1.显示4位验证码 注:大小写字母.数字混合 public static void main(String[] args) { String s="abcdefghijklmnopqrstu ...
- caffe中python接口的使用
下面是基于我自己的接口,我是用来分类一维数据的,可能不具通用性: (前提,你已经编译了caffe的python的接口) 添加 caffe塻块的搜索路径,当我们import caffe时,可以找到. 对 ...
- 整合Spring Data JPA与Spring MVC: 分页和排序
之前我们学习了如何使用Jpa访问关系型数据库.比较完整Spring MVC和JPA教程请见Spring Data JPA实战入门,Spring MVC实战入门. 通过Jpa大大简化了我们对数据库的开发 ...
- 谷歌 火狐 CSS兼容
@media screen and (-webkit-min-device-pixel-ratio:0){}谷歌@-moz-document url-prefix(){}火狐
- SPSS数据分析——t检验
SPSS中t检验全都集中在分析—比较均值菜单中.关于t检验再简单说一下,我们知道一个统计结果需要表达三部分内容,即集中性.变异性.显著性. 集中性的表现指标是均值变异的的表现指标是方差.标准差或标准误 ...
- Using Spring Boot without the parent POM
Using Spring Boot without the parent POM: 问题 spring boot项目一般情况下的parent如下: <parent> <groupId ...