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 ...
随机推荐
- QPS
你想建设一个能承受500万PV/每天的网站吗? 博客分类: 移动行业 PV 转自:http://elf8848.iteye.com/blog/967049 你想建设一个能承受500万PV/每天的网站 ...
- c++ 普通高精乘
//第一次提交全错了,太过相信自己了. //给我教训是注意循环后变量的值,和pascal不一样. //就不贴错误代码了. //codevs3118 高精度练习之除法 #include<cstdi ...
- Linux下的多线程编程
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...
- NOIP2005 等价表达式 解题报告
明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数表达式是和 ...
- myeclipse中导入的jquery文件报错(出现红叉叉,提示语法错误)
转自:http://blog.csdn.net/simplty/article/details/7661504
- IT公司100题-6-根据上排给出十个数,在其下排填出对应的十个数
问题描述: 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数要求下排每个数都是先前上排那十个数在下排出现的次数.上排的十个数如下:[0,1,2,3,4,5,6,7,8,9] 举一个例子, ...
- IE7的overflow失效的解决方法
IE7的position:relative bug今天遇到了一个相对定位(position:relaitve)引起的IE7中overflow:hidden失效的bug,特此记录!解决方法很简单,给父层 ...
- [pjsip]Pjlib中配置文件config.h解析
config_site.h 这个头文件包含在config.h中,用于引入平台?(site)/用户特定的配置以控制PJLIB的特性,用户需要自己生成这个文件. 譬如说我们要把PJLIB编译成DLL,那么 ...
- iphone获取当前运行进程列表
通过调用 sys/sysctl.h 读取系统内核获取进程列表 . 代码悦德财富:https://yuedecaifu.com 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 ...
- UITextView的字数限制 及 添加自定义PlaceHolder
- (void)textViewDidChange:(UITextView *)textView{ NSString *temp=textView.text; //字数超过限制数量时,进行截取替换 i ...