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. HDU 1394 树状数组求逆序对

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  2. hdu 4618 Palindrome Sub-Array

    http://acm.hdu.edu.cn/showproblem.php?pid=4618 直接DP+记忆化 虽然时间复杂度看起来是300^4 但实际执行起来要远远小于这个值 所有可以水过 代码: ...

  3. linux下挂载硬盘,解决阿里云挂载后重启消失的问题

    完整的阿里云挂载数据盘方法如下: 1.入手阿里云后查看有几块硬盘:(只显示概况,不显示分区情况) fdisk -l|grep Disk 2.查看硬盘分区 fdisk -l 如果有提示:disk /de ...

  4. 学好C++必须要注意的十八个问题

    转自  http://blog.chinaunix.net/uid-7396260-id-2056691.html 一.#include "filename.h"和#i nclud ...

  5. 解决python version 2.7 required,which was not find in the registry

    程序自动写注册表 http://www.vvivv.com/post-143.html 手工写 http://blog.csdn.net/baikaishui525/article/details/9 ...

  6. 关于Xcode调试的帖子,感觉不错,转来看看

    http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 http://www.raywenderlich.com/10505 ...

  7. 记录一些容易忘记的属性 -- UIButton

    //设置按钮文字字体(这个只在自定义button时有效)    btn1.titleLabel.font = [UIFont systemFontOfSize:30]; showsTouchWhenH ...

  8. (三)获取iphone的IMSI

    今天的任务是 iPhone上怎样获取 imsi 信息 来判断所属运营商,资料找了很久!总体有两种方案,但是其中一种好像不行 这里我都记录下来吧: 1: 这是使用coreTelephony.framew ...

  9. FZU1683 矩阵

    //Accepted 220 KB 359 ms #include <cstdio> #include <cstring> ; int pp; struct matrix { ...

  10. poj2193

    //Accepted 368K 532MS //线性dp //dp[i][j]表示前i位最后一个是j的排列数 //dp[i][j]=sum(dp[i-1][h]) h*2<=j #include ...