【树】Validate Binary Search Tree
需要注意的是,左子树的所有节点都要比根节点小,而非只是其左孩子比其小,右子树同样。这是很容易出错的一点是,很多人往往只考虑了每个根节点比其左孩子大比其右孩子小。如下面非二分查找树,如果只比较节点和其左右孩子的关系大小,它是满足的。
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的更多相关文章
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 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 ...
- [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 ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【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) ...
随机推荐
- swift的可选值(optional)
苹果那文档写了一大堆也没有好好的写一下可选值(optional)这个东西.就是在有一个“Optional Chaining”的章节,但是也不是很充分的说明.最后找了半天在“the basics”里墨迹 ...
- ctx.header
ctx.headers 获取所有的 header 信息,等同于 ctx.header. session session的中文翻译是“会话”,当用户打开某个web应用时,便与web服务器产生一次sess ...
- 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- Oracle EBS 快捷键
打开菜单 Help > Keyboard Help... 功能 快捷键 =================================== 1 ...
- CURL命令测试网站打开速度
curl -o /dev/null -s -w %{time_namelookup}:%{time_connect}:%{time_starttransfer}:%{time_total} http: ...
- 在一般处理程序清理cookie
清理cookie在ashx里面很奇怪,因为直接设置过期时间并不能成功,cookie还是会存在.所以需要添加一个同名的Cookie设置过期时间覆盖 HttpCookie cookie = null; / ...
- bash shell & 环境变量
root是没有~/.bashrc的,只. /etc/profile即可,/etc/profile和~/.bashrc的作用类似,只是作用域不同,都是写死的export,也有动态的脚本去设置命令和环境变 ...
- MongoDB .Net Driver(C#驱动) - 内嵌数组/嵌入文档的操作(增加、删除、修改、查询(Linq 分页))
目录 一.前言 1. 运行环境 二.前期准备工作 1. 创建 MongoDBContext MongoDb操作上下文类 2.创建测试类 3.创建测试代码 三.内嵌数组增加元素操作 1.Update.S ...
- 1、认识Redis
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库.Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库, ...
- kolla-ansible 重新部署 ceph-mon 组件
1.备份数据 [root@controller ~]# mv /var/lib/docker/volumes/ceph_mon /var/lib/docker/volumes/ceph_backup/ ...