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) ...
随机推荐
- Maven学习(一) -- 安装Maven及Eclipse中配置Maven
标签(空格分隔): 学习笔记 本文环境:Windows7, JDK1.7.0_76 安装及配置Maven环境变量 需要电脑中已经有Java环境 在控制台中输入:echo %JAVA_HOME%看是否能 ...
- Ubuntu 16.04 + Caffe
主要参考: https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide http://caffe.berke ...
- 让IE7 IE8支持CSS3 background-size属性
简介 CSS3 新增的 background-size 是一个很有用的属性,用于定义背景图片的尺寸,有了这个属性,你就可以任意指定背景图片的大小.其中最常用的值应该要数 cover 了,该值能让背景图 ...
- angularJS学习笔记之——搭建学习环境
学习AngularJS已经好几天了,从今天开始学习AngularJS环境搭建. 无论是Mac.Linux或Windows环境中,您均可遵循本教程学习编程. 第一步:安装Git Git是什么呢? Git ...
- 【转】gdb 调试段错误
[转]gdb 调试段错误 转自:blog.csdn.net/yangzhu1982/article/details/6318600 开发嵌入式Linux的时候经常会遇到segmentation fau ...
- JQuery的概述
一.什么是 jQuery 1.jQuery是一个JavaScript库,jQuery 极大地简化了 JavaScript 编程.它通过封装原生的JavaScript函数得到一整套定义好的方法. 2.它 ...
- nginx反向代理实现跨域请求
nginx反向代理实现跨域请求 跨域请求可以通过JSONP实现,缺点是需要修改被请求的服务器端代码进行配合,稍显麻烦通过在自己服务器上配置nginx的反向代理,可以轻松实现跨域请求 思路 示例服务器A ...
- 运行Maven是报错:No goals have been specified for this build
No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in t ...
- oracle表空间不足相关问题解决办法
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- linux 下mysql的安装,并设置必要的密码
首先,我使用的是redhat linux ,版本号为: [root@localhost init.d]# cat /proc/version Linux version - (bhcompile@po ...