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 ...
随机推荐
- location修改的时候报错解决办法
location修改的时候直接保存record,或者转换成DbObject都会报错,这样准换之后就好了if(key.equals("location")&&valu ...
- 《学习的艺术》 (The Art of Learning)——划小圈 (Making Smaller Circles)
“……我对象棋.武术以及范围更广的整个学习过程这三者的核心及相互关系的研究在一定程度上是受到了罗伯特.波席格<摩托车维护艺术>一书的启发.我决不会忘记指导我今后数年学习方式的那一幕.波席格 ...
- java链接mysql添加中文和模糊查询
如下内容为转载 http://sunshinechen2008.blog.163.com/blog/static/107585374201162442643967/ mysql如果不对乱码处理 ...
- sqlalchemy 映射的小例子
1.多张表映射到一个类 import pandas as pdfrom settings import DATABASESfrom sqlalchemy import create_engineimp ...
- PRG(Post/Redirect/Get)
转自:PRG(Post/Redirect/Get) 摘要: Post/Redirect/Get 简称PRG,是一种用来防止表单重复提交数据的一种Web设计模式 Post/Redirect/Get 简称 ...
- Sass与Compress实战:第三章
概要:这一章将介绍Compass如何使Web设计中最基础的部分——布局变得简单. 本章内容: ● 网格布局的基本原理以及何时使用网格布局 ● 使用Compass时的CSS网格布局框架选项 ● 使用排版 ...
- jQuery第九章
第九章 jQuery Mobile 一.HTML5.0简介 谈到Web设计,我们经常把Web分为三个层: (1)结构层:(2)表现层:(3)行为层. 对应的技术分别是: (1)HTML:(2)CSS: ...
- centos 7上搭建HDP2.3集群
centos 7上安装 cat /etc/redhat-release
- C# 语言规范_版本5.0 (第3章 基本概念)
1. 基本概念 1.1 应用程序启动 具有入口点 (entry point) 的程序集称为应用程序 (application).应用程序运行时,将创建新的应用程序域 (application doma ...
- C# using
我们知道 using 语句只不过是提供能确保正确使用 IDisposable 对象的方便语法. 1: using (IDisposable reader1 = new StreamReader(inp ...