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. .net core在Linux ARM板上运行

    最近接了个临时任务,给别的项目组的机器人平台上开发个小程序,那机器人上跑的是ARM平台,ubuntu的系统. 本来打算用C++写的,由于最近用.net core较多,鉴于其在linux平台良好的兼容性 ...

  2. hdu5094 Maze

    --就是爬管道-- 还好内存给的多-- 不然就不会做了-- #include<iostream> #include<map> #include<string> #i ...

  3. USBDM RS08/HCS08/HCS12/Coldfire V1,2,3,4/DSC/Kinetis Debugger and Programmer -- MC9S08JS16

    Introduction The attached files provide a port of a combined TBDML/OSBDM code to a MC9S08JS16 proces ...

  4. man命令使用

    如:man 2 read, 就可以查看read函数的文档

  5. 在Visual Studio中使用活动图描述业务流程

    当希望描述某个流程的时候,用活动图表示. 在项目中添加一个名称为"Shopping"的文件夹. 把"Orders Model"这个UML类图拖放到Shoppin ...

  6. Maven 使用了一个标准的目录结构和一个默认的构建生命周期。

    Maven 使用了一个标准的目录结构和一个默认的构建生命周期. 约定优于配置 当创建 Maven 工程时,Maven 会创建默认的工程结构.开发者只需要合理的放置文件,而在 pom.xml 中不再需要 ...

  7. 排序算法之归并排序(Mergesort)解析

    转自:http://www.cnblogs.com/ayqy/p/4050452.html   一.归并排序的优缺点(pros and cons) 耗费心思来理解它,总要有个理由吧: 归并排序的效率达 ...

  8. winform中的TreeView的数据绑定

    #region 绑定TreeView /// <summary> /// 绑定TreeView(利用TreeNode) /// </summary> /// <param ...

  9. [Android Pro] 由模块化到组件化(一)

    cp from : https://blog.csdn.net/dd864140130/article/details/53645290 在Android SDK一文中,我们谈到模块化和组件化,现在我 ...

  10. Asp.Net Core 静态文件目录操作

    一.默认静态文件处理 Asp.Net Core的默认处理方式,将所有的静态文件都放在wwwroot文件夹中 1.默认配置,在启动文件Startup中 public void Configure(IAp ...