[Algorithm] Check if a binary tree is binary search tree or not
not is or if Tree search Binary check
2023-09-14 09:00:49 时间
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(10); tree.root = root; const ll1 = tree.addLeft(5, tree.root); tree.addRight(16, tree.root); const ll2 = tree.addLeft(4, ll1); const lr2 = tree.addRight(7, ll1); tree.addLeft(1, ll2); tree.addRight(11, lr2); tree.addRight(16, 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(7); tree2.root = root2; const _ll1 = tree.addLeft(4, tree.root); tree.addRight(9, tree.root); tree.addLeft(1, _ll1); tree.addRight(6, _ll1); /* 7 / \ 4 9 / \ 1 6 */ const res2 = isBinarySearchTree(root2); // true console.log(res2);
相关文章
- xxx is not in the sudoers file.This incident will be reported.的解决方法
- docker报错Error response from daemon: Get https://registry-1.docker.io/v2/: x509: certificate has expired or is not yet valid
- 【异常】[ERROR] The cloud assistant is not installed on the ECS, or the cloud assistant is unavailable. cloudassistant is uninstall
- java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: service-one
- Bower : ENOGIT git is not installed or not in the PATH
- TypeError: 'bases' is null or not an object。IE8 bug 腐朽的对象
- Teamviewer ubuntu 提示 TeamViewer Daemon is not running
- Eslint:'$' is not defined
- zabbix server is not running: the information displayed may not be current
- ValueError: {0} is not a valid coordinate or range问题解决
- Where is ABAP Netweaver HTTP 304 not modified set
- Product ID Not in valid range
- $.countdown is not a function
- 成功解决NameError: name ‘norm‘ is not defined
- Matlab:成功解决The option is not valid. The options must be'double','native','default','omitnan', or'inc
- 成功解决SyntaxError: future feature annotations is not defined
- 已解决ERROR: Could not build wheels for opencv-python-headless, which is required to install
- 已解决note: This is an issue with the package mentioned above,not pip.
- 已解决(pip报错)WARNING: The repository located at mirrors .aliyun.com is not a trusted or secure host and
- 报错 java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp 原因
- 完美解决Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource的问题
- 完美解决coco数据集加载问题:get_coco_dataset.sh: line x: $‘r‘: command not found