【树】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) ...
随机推荐
- 8.使用Exists监控ZNode的三大Change事件
一. zookeeper是一个分布式的协调程序(所有程序都是通过订阅它来相互感知) 1. tcp(长链接) + watcher server ->client client ->ser ...
- Linux(一) - Unix&Linux 历史
Unix Unix 的诞生 Unix的历史可以追溯到20世纪60年代中期,当时麻省理工学院,AT&T,贝尔实验室和通用电气公司联合开发了一种名为Multics的操作系统,Multics 中存在 ...
- Excel2010画动态甘特图
哈哈!你居然真的看简介点进来啦,我也想八一八Henry gantt本人的故事,可是我查了好些资料,一个槽点都没有发现,不过人生经历还是蛮拼的: 此人活了58年,前半生就是一个中规中距的机械工程师&am ...
- nginx停止
- EF按时间范围条件查询
查询今日数据 db.Table.Where(d => System.Data.Entity.DbFunctions.DiffDays(d.Time, DateTime.Now) == )
- 使用Base64进行string的加密和解密
//字符串转bytes var ebytes = System.Text.Encoding.Default.GetBytes(keyWord); //bytes进行base64加密 var strBa ...
- C# Winform WPF DeskBand 窗体嵌入任务栏,在任务栏显示文字
最近写了个小程序,用于将固态硬盘的写入量等信息显示在任务栏,最开始使用Windows API也可以实现,但是当任务栏托盘增加的时候,会被遮盖,最终采用了DeskBand来实现,填了很多坑. 参考的Gi ...
- API Test Postman接口测试之高级篇2
API Test Postman接口测试之高级篇2 一.继承父类的设置: 二.导出及导入: 三.分享文档: 四.发布接口文档: 五.常用脚本: 右边框选的是一些常用的脚本,postman提供的,可以 ...
- Android 内存管理研究
1. 内存管理基础知识 http://www.cnblogs.com/xingfuzzhd/p/3485924.html 1. mImageView.setImageResource(R.drawab ...
- 【ocp-12c】最新Oracle OCP-071考试题库(40题)
40.(8-7) choose two Which two statements are true regarding views? (Choose two.) A) A simple view in ...