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的更多相关文章

  1. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  2. 【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) ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 【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 ...

  8. 【一天一道LeetCode】#98. Validate Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. [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 ...

随机推荐

  1. mac 连接mysql提示 Warning: mysqli::real_connect(): (HY000/2002): No such file or directory

    mac 连接mysql的时候提示 Warning: mysqli::real_connect(): (HY000/2002): No such file or directory [说明1]MAC下M ...

  2. 可滑动的ToggleButton(开关)

    2013-12-28 17:25:01 网上看到一篇关于可滑动的ToogleButton的文章,有代码,觉得挺好,但是不符合我的要求,因此在他的代码基础上改了一些.(作者看到了勿喷啊,实在找不到原文了 ...

  3. EntityFramework查询oracle数据库时报ora-12704: character set mismatch

    1.这段linq,执行期间报ora-12704:character set mismatch错误. var query = from m in ctx.MENU where (m.SUPER_MENU ...

  4. POJ 3237

    题目大意:指定一颗树上有3个操作:询问操作,询问a点和b点之间的路径上最长的那条边的长度:取反操作,将a点和b点之间的路径权值都取相反数:变化操作,把某条边的权值变成指定的值. #include &l ...

  5. 合理利用 vs2013的性能分析跟诊断

    选择对应的项目==> 我正常是选择采样 就包括里面的一些耗时.  挺好用的. 可以根据热路径 还有访问的占比.知道哪个环节占用的访问时间 还有性能耗能多. 可以点进去 跟踪跟修改

  6. julia生成指定格式的字符串.jl

    julia生成指定格式的字符串.jl """ julia生成指定格式的字符串.jl http://bbs.bathome.net/thread-39829-1-1.htm ...

  7. 获取hadoop的源码和通过eclipse关联hadoop的源码

    一.获取hadoop的源码 首先通过官网下载hadoop-2.5.2-src.tar.gz的软件包,下载好之后解压发现出现了一些错误,无法解压缩, 因此有部分源码我们无法解压 ,因此在这里我讲述一下如 ...

  8. hdu 2027

    ps:发现语文理解能力不行也是醉醉的....是每个测试实例空行....空!行!不是空格! 还有就是gets才能吸收空格,而scanf不能. 代码: #include "stdio.h&quo ...

  9. struts2和servlet同时用(访问servlet时被struts2过滤器拦截问题的解决)

    在同一个项目中间,如果既用到servlet有用了struts2的框架,运行项目时可能无法正常使用servlet,原因是在配置struts2的核心控制器时<url-pattern>/*< ...

  10. CSS样式选择器

    <!-- css样式选择器 HTML选择器 类选择器 ID选择器 关联选择器 组合选择器 伪元素选择器 selector{ /* selector是样式选择器 property:value; / ...