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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 泛型Binary Search Tree实现,And和STL map比较的经营业绩

    问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...

  4. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  5. 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 ...

  6. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  7. 【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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  2. DMA Stream/Channel Outputting via GPIOC[0..7]

    Ok, so quickly mashing up another example using a different TIM, DMA Stream/Channel for illustration ...

  3. 搭建基于crtmpserver的点播解决方案

    1. linux环境下build并启动crtmpserver 这部分可以参见我写的专项详解文章 <crtmpserver流媒体服务器的介绍与搭建> 和 <crtmpserver配置文 ...

  4. PGXZ-腾讯全功能分布式关系数据集群

    PGXZ-腾讯全功能分布式关系数据集群

  5. MySQL Workbench的安全更新模式

    MySQL Workbench上使用"DELETE FROM TABLE_E;"清空一个表时返回错误: Error Code: 1175. You are using safe u ...

  6. 培养iOS开发新人的一个思路

    坚持两个方法论: 1.发现问题的方法:(熟悉代码的过程) (1)照着一个完整的工程,从最基本的页面开始做起.不懂的地方就问,就查. (2)在阅读代码或拿到需求后要学会对问题进行分解.一个陌生的问题如果 ...

  7. 平台升级至nginx+Tomcat9.0.1(Spring5.0.1+velocity2.0+quartz-2.3.0)+redis集群

    在公司部份应用上 使用了 Tomcat9.0.1 稳定性还可以,由于将公司的集群服务也升级为 Tomcat9.0.1,下面我们来谈一下改变: 1:logging.properties 支持日志最大天数 ...

  8. javax.servlet不存在问题的解决

    产生这个问题的原因是这个包在WEB服务器里才有,J2SE中没有,应该在J2EE中才有.因此必须告诉编译器这个包的位置. 正确的解决方案如下: 1. 搜索servlet-api.jar. 这个包应该在T ...

  9. 血族第四季/全集The Strain迅雷下载

    当第四季开始时,故事时间已经过去九个月.世界陷入黑暗,斯特里高伊吸血鬼控制了一切.第三季结尾的爆炸引发了一场全球核灾难,核冬天的到来令地表变得暗无天日,斯特里高伊获得解放.它们大白天也能出来活动,帮助 ...

  10. [Web 前端] this作用域问题

    如何不用这种写法:理想的写法是this.setState({ .... }) 可能是我没有描述清楚,我不想用这种学法而已,这样多了一个变量,我觉得很不舒服.我尝试了先把 setState 赋值到变量里 ...