[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 ...
随机推荐
- STM32 通用定时器的几种配置方式
STM32 通用定时器的几种配置方式 //------------------------------------------------------------------------------ ...
- 搭建《深入Linux内核架构》的Linux环境
作者 彭东林 pengdonglin137@163.com 软件 Host: Ubuntu14.04 64 Qemu 2.8.0 Linux 2.6.24 busybox 1.24.2 gcc 4.4 ...
- 快速将wax配置到项目中进行lua开发
通过Finder浏览到你保存该项目的文件夹.创建三个新的文件夹:wax.scripts和Classes. 第一:首先,下载源代码的压缩包.Wax放在GitHub上(https://github.com ...
- 为什么用freemarker视图?
在java领域,表现层技术主要有三种:jsp.freemarker.velocity. jsp是大家最熟悉的技术优点:1.功能强大,可以写java代码2.支持jsp标签(jsp tag)3.支持表达式 ...
- 在MFC内竟然不可以解析“Mat”
调试了好久,在MFC内竟然不可以解析“Mat” 总是显示"Mat"没有定义,改动include.lib.依赖库, 不断地改动: #include "opencv2/img ...
- TX1 flash backup & restore
备份:耗时2.5小时 ./tegraflash.py --bl cboot.bin --applet nvtboot_recovery.bin --chip 0x21 --cmd "read ...
- 根据ImageView的大小来压缩Bitmap,避免OOM
Bitmap是引起OOM的罪魁祸首之一,当我们从网络上下载图片的时候无法知道网络图片的准确大小,所以为了节约内存,一般会在服务器上缓存一个缩略图,提升下载速度.除此之外,我们还可以在本地显示图片前将图 ...
- SecureCRT SSH 语法高亮
主要原因 1.term类型不对,不支持彩色.在secureCRT上设置 Options->SessionOptions ->Emulation,然后把Terminal类型改成xterm,并 ...
- [转]PHP: 深入pack/unpack
From : http://my.oschina.net/goal/blog/195749 http://www.w3school.com.cn/php/func_misc_pack.asp PHP作 ...
- [转]Hadoop Hive sql语法详解
转自 : http://blog.csdn.net/hguisu/article/details/7256833 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式 ...