[Algorithm] Check if a binary tree is binary search tree or not
What is Binary Search Tree (BST)
A binary tree in which for each node, value of all the nodes in left subtree is less or equal and value of all the nodes in right subtree is greater

The idea:
We can use set boundry for each node. We take C tree for example:
For root node, the B (bountry) = [MIN, MAX]
node 4: B = [MIN, 7] // cannot be greater than 7
node 1: B = [MIN, 4]
node 6: B = [4, 7] // cannot less than 4, but should less than 7.
node 9: B = [7, MAX] // cannot less than 7
function Node(val) {
return {
val,
left: null,
right: null
};
}
function Tree() {
return {
root: null,
addLeft(val, root) {
const node = Node(val);
root.left = node;
return root.left;
},
addRight(val, root) {
const node = Node(val);
root.right = node;
return root.right;
}
};
}
function isBinarySearchTree(root) {
function helper(root, max, min) {
if (root == null) {
return true;
}
if (
root.val >= min &&
root.val <= max &&
helper(root.left, root.val, min) &&
helper(root.right, max, root.val)
) {
return true;
} else {
return false;
}
}
return helper(root, Number.MAX_VALUE, Number.MIN_VALUE);
}
const tree = new Tree();
const root = Node();
tree.root = root;
const ll1 = tree.addLeft(, tree.root);
tree.addRight(, tree.root);
const ll2 = tree.addLeft(, ll1);
const lr2 = tree.addRight(, ll1);
tree.addLeft(, ll2);
tree.addRight(, lr2);
tree.addRight(, tree.root);
/*
10
/ \
5 16
/ \
4 7
/ \
1 11
11 is greater than 10, which is false
*/
const res1 = isBinarySearchTree(root); // false
console.log(res1);
const tree2 = new Tree();
const root2 = Node();
tree2.root = root2;
const _ll1 = tree.addLeft(, tree.root);
tree.addRight(, tree.root);
tree.addLeft(, _ll1);
tree.addRight(, _ll1);
/*
7
/ \
4 9
/ \
1 6
*/
const res2 = isBinarySearchTree(root2); // true
console.log(res2);
[Algorithm] Check if a binary tree is binary search tree or not的更多相关文章
- [Algorithm] Delete a node from Binary Search Tree
The solution for the problem can be divided into three cases: case 1: if the delete node is leaf nod ...
- [Algorithm] Inorder Successor in a binary search tree
For the given tree, in order traverse is: visit left side root visit right side // 6,8,10,11,12,15,1 ...
- 泛型Binary Search Tree实现,And和STL map比较的经营业绩
问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- leetcode -- Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 【LeetCode OJ】Convert Sorted List to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ We design a ...
- [CareerCup] 4.3 Create Minimal Binary Search Tree 创建最小二叉搜索树
4.3 Given a sorted (increasing order) array with unique integer elements, write an algorithm to crea ...
- [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 ...
随机推荐
- HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- Echarts学习记录——如何去掉网格线及网格区域颜色
关键属性 splitLine和splitArea,可以设置相关的属性 示例代码 <!DOCTYPE html> <html lang="en"> <h ...
- SSH 证书登录(实例详解)
SSH 证书登录(实例详解) 客户端通过私钥登录 ssh 服务器 CentOS 7 SSH 使用证书登录 使用私钥 ssh 登陆 CentOS
- 打开IPHONE的sms.db短信文件 方法
先将导出的sms.db文件改名为sms.sqlite再下载个火狐浏览器<IGNORE_JS_OP style="WIDOWS: 2; TEXT-TRANSFORM: none; BAC ...
- 【linux】在linux上生成SSH-key 简单原理介绍+生成步骤
1.首先什么是SSH Secure Shell (SSH) 是一个允许两台电脑之间通过安全的连接进行数据交换的网络协议.通过加密保证了数据的保密性和完整性.SSH采用公钥加密技术来验证远程主机,以及( ...
- codeforces Round #259(div2) C解题报告
C. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- OCP-1Z0-051-题目解析-第22题
22. You need to create a table for a banking application. One of the columns in the table has the fo ...
- MyBatis入参类型是List时判断非空
一.参数list时,先判断是否为空,否则会报错. 二.mybatis ${}与#{}的区别 简单来说#{} 解析的是占位符?可以防止SQL注入, 比如打印出来的语句 select * from tab ...
- 利用svn log命令实现的资源版本更新
无论页游或是手游都需要经常进行更新,而每一次更新几乎都是一部血泪吏.这里重点介绍一下前端资源打包的简化操作.目前2D手游主流都采用了cocos2d-x 绑lua的做法,因为lua相当于一种资源可以进行 ...
- Java并发编程的艺术(九)——批量获取多条线程的执行结果
当向线程池提交callable任务后,我们可能需要一次性获取所有返回结果,有三种处理方法. 方法一:自己维护返回结果 // 创建一个线程池 ExecutorService executorServic ...