【树】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) ...
随机推荐
- Opencascade、OpenGL和OpenSceneGraph的区别与联系
OpenGL只是三维显示 Openscenegraph基于场景图的概念,它提供一个在OpenGL之上的面向对象的框架,从而能把开发者从实现和优化底层图形的调用中解脱出来 Opencascade更适合算 ...
- 20169205 2016-2017-2 实验二nmap的使用与分析
20169205 2016-2017-2 实验二Nmap的使用与分析 实验所用知识总结 Nmap扫描基础 当用户对Nmap工具了解后,即可使用该工具实施扫描.通过上一章的介绍,用户可知Nmap工具可以 ...
- VUE 学习笔记 三 模板语法
1.插值 a.文本 数据绑定最常见的形式就是使用“Mustache”语法 (双大括号) 的文本插值 <span>Message: {{ msg }}</span> v-once ...
- 限制html文本框input只能输入数字和小数点
代码: <input type="text" class="txt" name="qty" value="" on ...
- VisualStudio2017 远程 调试 IIS 服务器 web网站
小伙伴们,本次测试好好的程序发布到服务器挂到IIS后我勒个*,,, 神马情况,为啥和我本地运行结果不一致,Fuc*... 没遇到的小伙伴估计也看不到此篇文章了吧,Log日志调试,嗯 不错,good i ...
- Mysql修改字段类型
mysql 修改字段长度 alter table news modify column title varchar(130); alter table 表名 modify column 字段名 类型 ...
- bootstrap table 的searchParam参数传递
bootstrap table 的searchParam自定义参数传递 Bootstrap Table返回的数据为value 和 rows Long total代表的是多少条(总数) List< ...
- k8s 常用指令
kubectl rolling-update redis-master --image=redis-master:2.0 基于镜像灰度发布 推荐链接
- TOJ1398正方形的编成 或者 POJ2362
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> ...
- You can't specify target table 'e' for update in FROM clause
UPDATE emp e SET e.salary=e.salary+7 WHERE e.id IN(SELECT e1.id FROM emp e1,dept d WHERE e1.dep_id=d ...