需要注意的是,左子树的所有节点都要比根节点小,而非只是其左孩子比其小,右子树同样。这是很容易出错的一点是,很多人往往只考虑了每个根节点比其左孩子大比其右孩子小。如下面非二分查找树,如果只比较节点和其左右孩子的关系大小,它是满足的。

5
  /     \
4      10
      /      \
    3        11

错误代码:

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function(root) {
if(root==null){
return true;
} if (root.left != null && root.val <= root.left.val){
return false;
}
if (root.right != null && root.val >= root.right.val){
return false;
}
return isValidBST(root.left) && isValidBST(root.right); };

正确解法

思路:利用中序遍历将二叉树中所有值存入一个数组,我们知道,二插搜索树用中续遍历的所有值都符合从大到小的顺序,所以,我们就可以通过判断数组中的值是否是从大到小顺序来判断二叉树是否为二插搜索树。

代码如下:

var isValidBST = function(root) {
if(root==null){
return true;
} function asc(x, y) {
return x > y;
}; var result=inOrderTraversal(root);var temp=result.concat();
result.sort(asc);for(var i=0;i<result.length;i++){
if (result[i]==result[i+1]){
return false;
}
} if(result.toString()==temp.toString()){
return true;
}else{
return false;
}
}; var inOrderTraversal=function(root){
if(root==null){
return [];
}
if(root.left==null&&root.right==null){
return [root.val];
} var p=root,result=[],stack=[];
while(p||stack.length!=0){
if(p!=null){
stack.push(p);
p=p.left;
}else{
p=stack.pop();
result.push(p.val);
p=p.right;
}
}
return result;
}

注意几个点:

  • result.concat()目的是为了简历result数组的副本
  • arr.sort()是在原有数组上进行修改
  • 利用toString()方法可以比较两个数组值是否相等

【树】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. Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  3. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  4. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

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

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

  7. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  8. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  9. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

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

随机推荐

  1. MySQL性能调优与架构设计——第10章 MySQL数据库Schema设计的性能优化

    第10章 MySQL Server性能优化 前言: 本章主要通过针对MySQL Server(mysqld)相关实现机制的分析,得到一些相应的优化建议.主要涉及MySQL的安装以及相关参数设置的优化, ...

  2. python + selenium + Js 处理轮动条

    selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 常见场景: 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. 这时候需要借 ...

  3. 传智播客.NET视频学习课件

    传智播客.NET视频学习课件访问.NET网站了解更多课程详情http://net.itcast.cn(小提示:为什么本书中超链接打不开?)此套课件是伴随 传智播客.net实况教学视频 (小提示:为什么 ...

  4. struts中request传递中文乱码问题

    系统本来是好好地,这两天升级后,各种问题不断,总而言之,一句话,心惊胆战. 今天,搜索任何中文,都是有乱码,在action中转码就ok了.公司系统那么多action,都转码,要累死吧.配置的过滤器都不 ...

  5. 去掉easyui tree 的默认图标

    $(".tree-icon,.tree-file").removeClass("tree-icon tree-file"); $(".tree-ico ...

  6. winform 版本号比较

    Version now_v = new Version(strval); Version load_v = new Version(model.version.ToString()); if (now ...

  7. “全栈2019”22篇Java异常学习资料及总结

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"22篇Ja ...

  8. “全栈2019”Java异常第十六章:Throwable详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  9. python反转列表的几种方法

    一.使用reversed()函数 a = [1, 2, 3, 4] b = list(reversed(a)) 注意:reversed()函数返回的是一个迭代器,而不是一个List,需要再使用List ...

  10. 浅谈HashMap原理,记录entrySet中的一些疑问

    HashMap的底层的一些变量: transient Node<K,V>[] table; //存储数据的Node数组 transient Set<java.util.Map.Ent ...