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. getsockname()/getpeername()函数第一次被调用得到0.0.0.0结果

    int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen); getsockname() returns the cu ...

  2. poj 2429 GCD &amp; LCM Inverse 【java】+【数学】

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9928   Accepted:  ...

  3. Unity3D实践系列03,使用Visual Studio编写脚本与调试

    在Unity3D中,只有把脚本赋予Scene中的GameObject,脚本才会得以执行. 添加Camera类型的GameObject. Unity3D默认使用"MonoDevelop&quo ...

  4. C#编程(五十)----------栈

    栈 栈与队列是一个非常类似的容器,他们的区别在于队列是先进先出,而栈是后进先出. Stack与Stack<T>,像队列一样,栈也提供了泛型与非泛型版本. Stack的方法: 方法 说明 P ...

  5. spring源码之—Assert.notNull

    org.springframework.util.Assert Assert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了. 就是断定某一个实际的值就为自己预期想得到的 ...

  6. ibatis.net:第七天,QueryWithRowDelegate

    xml <statement id="FindOrdersByCustomer" parameterClass="string" resultClass= ...

  7. Linux网络之设备接口层:发送数据包流程dev_queue_xmit

    转自:http://blog.csdn.net/wdscq1234/article/details/51926808 写在前面 本文主要是分析kernel-3.8的源代码,主要集中在Network的n ...

  8. Java 如何实现在线预览文档及修改(Office文件)

    测试地址: https://sms.idxkj.cn 用户名:aa 密码:123456

  9. 转: MySQL5.7 ERROR 1142 (42000)问题

    1,mysql全库导入报错 [root@dev_121_21 ~]# mysql--socket=/usr/local/mysql/mysql.sock --default-character-set ...

  10. 罪恶黑名单第一季/全集The Blacklist迅雷下载

    罪恶黑名单 第一季 The Blacklist Season 1 (2013)本季看点:几十年来,绰号「红魔」(Red)的前军事情报官员Raymond Reddington(James Spader) ...