[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 ...
随机推荐
- linux 内核开发环境搭建
http://blog.csdn.net/jemmy858585/article/details/46710299
- lodash用法系列(5),链式
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- 如何修改Oracle Enterprise Linux时区?
修改/etc/sysconfig/clock [root@psdyy-2 ~]# cat /etc/sysconfig/clock ZONE="Asia/Shanghai" UTC ...
- 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作
spring boot 2.X集成ES 进行CRUD操作 完整版 内容包括: ============================================================ ...
- git 两个中心仓库上的分支 merge
首先在一个中心仓库里面添加另外一个仓库的所有分支. 命令: git remote add Cangku2 https://github.com/abc/abc.git git fetch 这之后在使用 ...
- SharePoint Online 创建和使用栏
前言 本文介绍如何在Office 365中创建和使用栏. 正文 通过登录地址登录到Office 365的SharePoint Online站点中,我们可以在右上角的设置菜单中,进入网站内容: 找到我们 ...
- [Hook] 免root,自己进程内,binder hook (ClipboardManager)
cp from : http://weishu.me/2016/02/16/understand-plugin-framework-binder-hook/ Android系统通过Binder机制给应 ...
- SVG渲染顺序及z轴显示问题(zIndex)
SVG是严格按照定义元素的顺序来渲染的,这个与HTML靠z-index值来控制分层不一样. 在SVG中,写在前面的元素先被渲染,写在后面的元素后被渲染.后渲染的元素会覆盖前面的元素,虽然有时候受透明度 ...
- Java命令学习系列(六)——jinfo
jinfo可以输出java进程.core文件或远程debug服务器的配置信息.这些配置信息包括JAVA系统参数及命令行参数,如果进程运行在64位虚拟机上,需要指明-J-d64参数,如:jinfo -J ...
- 层次聚类 Hierarchical Clustering
-------------------------------- 不管是GMM,还是k-means,都面临一个问题,就是k的个数如何选取?比如在bag-of-words模型中,用k-means训练码书 ...