From aa83ce62e9018a89ee84b152cc7ef798b515fdf6 Mon Sep 17 00:00:00 2001 From: yige Date: Tue, 5 May 2020 18:25:52 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E9=AA=8C=E8=AF=81=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++ src/tree/validate-binary-search-tree.js | 16 +++++++ test/tree/Tree.js | 40 +++++++++++++++++ test/tree/TreeNode.js | 7 +++ test/tree/binary-tree-right-side-view.test.js | 44 +------------------ test/tree/validate-binary-search-tree.test.js | 12 +++++ 6 files changed, 82 insertions(+), 42 deletions(-) create mode 100644 src/tree/validate-binary-search-tree.js create mode 100644 test/tree/Tree.js create mode 100644 test/tree/TreeNode.js create mode 100644 test/tree/validate-binary-search-tree.test.js diff --git a/README.md b/README.md index a0036ef..8bae320 100644 --- a/README.md +++ b/README.md @@ -281,6 +281,11 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 199. 二叉树的右视图 https://leetcode-cn.com/problems/binary-tree-right-side-view/ - LintCode 760. 二叉树的右视图 https://www.lintcode.com/problem/binary-tree-right-side-view/description +- [验证二叉搜索树](src/tree/validate-binary-search-tree.js) + + - LeetCode 98. 验证二叉搜索树 https://leetcode-cn.com/problems/validate-binary-search-tree/ + - LintCode 95. 验证二叉查找树 https://www.lintcode.com/problem/validate-binary-search-tree/ + ## 链表 - [合并K个排序链表](src/list/merge-k-sorted-lists.js) diff --git a/src/tree/validate-binary-search-tree.js b/src/tree/validate-binary-search-tree.js new file mode 100644 index 0000000..0f21705 --- /dev/null +++ b/src/tree/validate-binary-search-tree.js @@ -0,0 +1,16 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +export const isValidBST = function (root, lower = -Infinity, upper = Infinity) { + if (root === null) return true + if (root.val <= lower || root.val >= upper) return false + return isValidBST(root.left, lower, root.val) && isValidBST(root.right, root.val, upper) +} diff --git a/test/tree/Tree.js b/test/tree/Tree.js new file mode 100644 index 0000000..be7d3d2 --- /dev/null +++ b/test/tree/Tree.js @@ -0,0 +1,40 @@ +import TreeNode from './TreeNode' + +export default class Tree { + constructor () { + this.root = null + this.queue = [] + this.insertNum = 0 + } + + insert (val) { + this.insertNum++ // 插入次数加1 + const node = (!val && typeof val === 'object') ? null : new TreeNode(val) // 判断是否为空节点 + + if (!this.root) { // 判断根节点是否存在 + this.root = node // 插入根节点 + node && this.queue.push(this.root) // 非空节点入列 + } else { // 插入非根节点 + const parent = this.queue[0] // 被插入的父节点 + if (!(this.insertNum % 2)) { // 通过插入次数判断左右 + parent.left = node // 插入左边 + node && this.queue.push(parent.left) // 非空节点入列 + } else { + parent.right = node // 插入右边 + node && this.queue.push(parent.right) // 非空节点入列 + this.queue.shift() // 当前父节点parent 已经不可能再插入子节点,故出列 + } + } + return this + } + + static arrToTree (arr) { + const tree = new Tree() + + for (const n in arr) { + tree.insert(arr[n]) + } + + return tree.root + } +} diff --git a/test/tree/TreeNode.js b/test/tree/TreeNode.js new file mode 100644 index 0000000..2e49241 --- /dev/null +++ b/test/tree/TreeNode.js @@ -0,0 +1,7 @@ +export default class TreeNode { + constructor (val) { + this.val = val + this.left = null + this.right = null + } +} diff --git a/test/tree/binary-tree-right-side-view.test.js b/test/tree/binary-tree-right-side-view.test.js index 1515622..7ff6647 100644 --- a/test/tree/binary-tree-right-side-view.test.js +++ b/test/tree/binary-tree-right-side-view.test.js @@ -1,48 +1,8 @@ import { rightSideView } from '../../src/tree/binary-tree-right-side-view' +import Tree from './Tree' test('二叉树的右视图', () => { - function TreeNode (val) { - this.val = val - this.left = null - this.right = null - } - - class Tree { - constructor () { - this.root = null - this.queue = [] - this.insertNum = 0 - } - - insert (val) { - this.insertNum++ // 插入次数加1 - const node = (!val && typeof val === 'object') ? null : new TreeNode(val) // 判断是否为空节点 - - if (!this.root) { // 判断根节点是否存在 - this.root = node // 插入根节点 - node && this.queue.push(this.root) // 非空节点入列 - } else { // 插入非根节点 - const parent = this.queue[0] // 被插入的父节点 - if (!(this.insertNum % 2)) { // 通过插入次数判断左右 - parent.left = node // 插入左边 - node && this.queue.push(parent.left) // 非空节点入列 - } else { - parent.right = node // 插入右边 - node && this.queue.push(parent.right) // 非空节点入列 - this.queue.shift() // 当前父节点parent 已经不可能再插入子节点,故出列 - } - } - return this - } - } - const arr = [1, 2, 3, null, 5, null, 4] - const tree = new Tree() - - for (const n in arr) { - tree.insert(arr[n]) - } - - expect(rightSideView(tree.root)).toEqual([1, 3, 4]) + expect(rightSideView(Tree.arrToTree(arr))).toEqual([1, 3, 4]) }) diff --git a/test/tree/validate-binary-search-tree.test.js b/test/tree/validate-binary-search-tree.test.js new file mode 100644 index 0000000..4a90882 --- /dev/null +++ b/test/tree/validate-binary-search-tree.test.js @@ -0,0 +1,12 @@ +import { isValidBST } from '../../src/tree/validate-binary-search-tree' +import Tree from './Tree' + +test('验证二叉搜索树', () => { + expect(isValidBST(Tree.arrToTree([1, 1]))).toBe(false) + expect(isValidBST(Tree.arrToTree([2, 1, 3]))).toBe(true) + expect(isValidBST(Tree.arrToTree([5, 1, 4, null, null, 3, 6]))).toBe(false) + expect(isValidBST(Tree.arrToTree([-1]))).toBe(true) + expect(isValidBST(Tree.arrToTree([2, 1, 4, null, null, 3, 5]))).toBe(true) + expect(isValidBST(null)).toBe(true) + expect(isValidBST(Tree.arrToTree([10, 5, 15, null, null, 6, 20]))).toBe(false) +})