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. Technical Information ARM-related JTAG / SWD / SWV / ETM Target Interfaces

    https://www.computex.co.jp/eg/products/pdf/technical_pdf/arm_if01_gijutsu_eng.pdf

  2. LINUX 源码+内核所有参数说明

    http://www.cnblogs.com/tolimit/p/5065761.html

  3. SQL 查询逻辑处理顺序

    http://www.cnblogs.com/lyhabc/articles/3912608.html http://blog.csdn.net/lanxu_yy/article/details/62 ...

  4. C++中的vector&find_if

     <STL應用> vector & find_if 看到有人問有個名為C的struct如下 code: struct C { int v1; int v2; }; 應用在vecto ...

  5. Mustache.js语法

    看了Mustache的github,学学此中的语法,做个笔记 1.简单的变量调换:{{name}} 1 var data = { "name": "Willy" ...

  6. 使用Axure RP原型设计实践03,制作一个登录界面的原型

    本篇体验做一个登录界面的原型. 登录页 首先在Page Style里为页面设置背景色. 如果想在页面中加图片,就把Image部件拖入页面,并设置x和y轴.双击页面中的Image部件可以导入图片.在Im ...

  7. ArcEngine二次开发错误编码对照表(转)

    阅读数:3323 每当我们在进行AE开发,出现错误时经常会出现错误代码,但是我们并不知道它到底代表什么意思,这里的而错误编码我们可以对照着找到我们需要的时候常详细信息(问题是,经常还是会出现没有错误编 ...

  8. android studio build.gradle中 project.ANDROID_BUILD_SDK_VERSION

    1.メニューの [File] -> [Import Module]2.Source directory に先ほど解凍したディレクトリを指定3.「facebook」 を選択した状態に Finish ...

  9. String Matching(poj1580)

    /*String Matching Description It's easy to tell if two words are identical - just check the letters. ...

  10. SharePoint Set-SPUser 命令拒绝访问

    · 前言 最近碰到一个问题,由于User Profile Service服务有问题,用户信息无法更新.所以,想到Set-SPUser命令可以更新,于是乎找到这个命令,但是更新的时候发现拒绝访问的错误. ...