Validate Binary Search Tree,判断是否是二叉排序树
算法分析:两种方法,一种是中序遍历,然后得到一个序列,看序列是否是有序的。第二种,是用递归。
中序遍历:
public class Solution {
List<Integer> list = new ArrayList<>();
public boolean isValidBST(TreeNode root) {
if(root == null)
{
return true;
}
inorderTraversal(root);
for(int i = 0; i < list.size() - 1; i ++)
{
if(list.get(i) >= list.get(i+1))
{
return false;
}
}
return true;
}
public void inorderTraversal(TreeNode root)
{
if(root == null)
{
return ;
}
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
}
}
递归:
public boolean isValidBST3(TreeNode root) {
return isValidBST(root, null, null);
}
public boolean isValidBST(TreeNode root, Integer min, Integer max)
{
if(root == null)
{
return true;
}
if(min != null && root.val <= min)
{
return false;
}
if(max != null && root.val >= max)
{
return false;
}
return isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max);
}
Validate Binary Search Tree,判断是否是二叉排序树的更多相关文章
- [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树
判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...
- 【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
题目链接: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) ...
- 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 (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LintCode 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 ...
随机推荐
- 动态设置progressBar的进度
progressDrawable = this.getResources().getDrawable(R.drawable.image); progressDrawable.setBounds(mSe ...
- 170509、文本编辑器编写的shell脚本在linux下无法执行的解决方法
今天碰到一个奇怪的问题,编写好的shell脚本再linux上执行一直提示找不到文件或目录,后来想想是文本编辑器的问题,记录下来!!! 1.查看当前文本格式 Notepad++界面中,在右下角有文件格式 ...
- CSU 1808 地铁 (Dijkstra)
Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci ...
- 【Python】自动化测试框架-共通方法汇总
1.滚动滚动条(有的时候页面元素element取得对但是并没有回显正确的数据,可能是因为页面第一次加载很慢,所以页面可能做了滚动到哪里就加载到哪里的效果,此刻我们就需要用到滚动条自动滚动这段代码让页面 ...
- makefile中ifeq与ifneq dev/null和dev/zero简介 dd命令
ifeq语法是ifeq "<arg1>;" "<arg2>;" ,功能是比较参数“arg1”和“arg2”的值是否相同,相同时为1 i ...
- Know that more adidas NMD Singapore colorways are coming
The adidas NMD Singapore continues to be the right silhouette for summer time because of a mix of a ...
- Educational Codeforces Round 54 (Rated for Div. 2) Solution
A - Minimizing the String solved 题意:给出一个字符串,可以移掉最多一个字符,在所有可能性中选取一个字典序最小的. 思路:显然,一定可以移掉一个字符,如果移掉的字符的后 ...
- uva1292 树形dp
这题说的是给了一个n个节点的一棵树,然后 你 从 这 棵 树 的 n 个 节点中 选择 尽量少的 点使得 每条边都至少有一个 士兵看守 dp[0][i]+=dp[1][j] dp[1][i]+=min ...
- 根据源Excel文件,新建Excel文件
/** * 描述:根据源Excel文件,创建新的Excel文件 * @param excelFile * @throws CheckException */public static void cre ...
- web上的复制
你可能曾经尝试过复制网页上的一些文字,得到的却是令人沮丧的的结果.这篇文章介绍相关的内容. 不是真正的文字 这可能是最常见的问题,很多人尝试对一张带有文字的图片进行像文字那样的选择,复制当然不行了. ...