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.
思路:中序排列为排序好的数组(数组元素一次增大);数组中不能有重复元素。
代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) { if(root==null||(root.left==null&&root.right==null))
return true;
ArrayList<Integer> list=ParentAndSon(root);
int[] nums=new int[list.size()];
int[] nums1=new int[list.size()];
for(int i=0;i<list.size();i++)
{
nums[i]=list.get(i);
nums1[i]=list.get(i);
}
Arrays.sort(nums); if(nums[0]!=nums1[0])
return false;
for(int i=1;i<nums.length;i++)
{
if(nums1[i]!=nums[i]||nums[i]==nums[i-1])
return false; }
return true;
}
public ArrayList<Integer> ParentAndSon(TreeNode root){
ArrayList<Integer> list=new ArrayList<>();
if(root.left!=null)
list.addAll(ParentAndSon(root.left)); list.add(root.val); if(root.right!=null)
list.addAll(ParentAndSon(root.right));
return list;
}
}
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 验证二叉搜索树
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
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 ----- java
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 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 ...
- 【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】#98. Validate Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 ...
随机推荐
- PHP 长字符串替换操作性能(替换多换注释的代码)
因为写一个css合并工具,去掉注释时,发现正则表达式很难写,最后,还是用php来实现吧,但是一不小心写出来的代码居然报内存超出可用的128m!! 原因是因为我找到/*和*/之后 $str=substr ...
- hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏
problem description http://acm.hdu.edu.cn/showproblem.php?pid=1030 #include <cstdio> #include ...
- 100个iOS开发/设计面试题汇总
常见问题 你昨天/这周学习了什么? 你为什么热衷于软件开发? 你对哪一种控制系统比较熟悉? 是否参与过GitHub项目? 是否参与过GitHub或其他同类型网站的iOS开源项目? 请描述一下你的iOS ...
- exec方法
如果 exec 方法没有找到匹配,将返回 null.如果找到匹配项,则 exec 方法返回一个数组,并将更新全局 RegExp 对象的属性以反映匹配结果.数组元素 0 包含了完整的匹配项,而元素 1 ...
- Geetest 极验验证 验证图片拼图
今天要求做一个跟魅族官网登陆的一个验证效果一样的界面 是一个拖动滑动图片进行拼图 那个效果看着很好,刚开始拿到不知道好不好做 从网上搜资料发现这是一种“极验验证码” 让用户通过滑动拼图来进行验证. 网 ...
- Form表单的操作
form对象 <form name=“form1” action=“login.php” method=“post”></form> form对象的属性 name:表单名称 m ...
- mysql 批量创建表,利用存储过程
最近根据需求,需要提前创建一批日志表,以日期结尾,每天创建一张,例如XXX20160530,请参考如下: BEGIN DECLARE `sName` VARCHAR(128); DECLAR ...
- Combination Sum 和Combination Sum II
这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在 ...
- php大力力 [018节]如何联系大力力
有事儿就注册博客园,给我发 博客园站内的 短消息呗,唉,没有人联系我呀,啦啦啦,爱我爱我,快点爱我 2015-08-26 php大力力018.如何联系大力力
- ubuntu 新系统需要做的事
1 : 打开语言支持 开始补齐并且选择自己需要的语言包 . 2 : 搜索计算机 输入 update 找到软件更新器 更新软件库 . 然后打开ubuntu自带软件安装工具下载自己想要的软件(没有更新之前 ...