From 146b33a651d9b8298d3af0597981ae08d4704079 Mon Sep 17 00:00:00 2001 From: yi-ge Date: Mon, 6 Jul 2020 07:58:05 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E5=B0=86=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 61 ++++++++++--------- ...vert-sorted-array-to-binary-search-tree.js | 26 ++++++++ ...sorted-array-to-binary-search-tree.test.js | 5 ++ 3 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 src/tree/convert-sorted-array-to-binary-search-tree.js create mode 100644 test/tree/convert-sorted-array-to-binary-search-tree.test.js diff --git a/README.md b/README.md index d557fb5..506afc7 100644 --- a/README.md +++ b/README.md @@ -63,13 +63,13 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 179. 最大数 - LintCode 184. 最大数 -- [实现string2int()函数](src/string/string2int.js) +- [实现 string2int()函数](src/string/string2int.js) - - 面试题参考思路,不严谨实现 廖雪峰 不要使用JavaScript内置的parseInt()函数,利用map和reduce操作实现一个string2int()函数。 + - 面试题参考思路,不严谨实现 廖雪峰 不要使用 JavaScript 内置的 parseInt()函数,利用 map 和 reduce 操作实现一个 string2int()函数。 - [左旋转字符串](src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js) - - LeetCode 面试题58 - II. 左旋转字符串 + - LeetCode 面试题 58 - II. 左旋转字符串 - [最后一个单词的长度](src/string/length-of-last-word.js) @@ -91,7 +91,7 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 680. 验证回文字符串 Ⅱ - LintCode 891. 有效回文 II -- [IP地址无效化](src/string/defanging-an-ip-address.js) +- [IP 地址无效化](src/string/defanging-an-ip-address.js) - LeetCode 1108. IP 地址无效化 @@ -268,7 +268,7 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - [逆序对](src/array/reverse-pairs.js) - - LeetCode 面试题51. 数组中的逆序对 + - LeetCode 面试题 51. 数组中的逆序对 - LintCode 532. 逆序对 - [搜索旋转排序数组](src/array/search-in-rotated-sorted-array.js) @@ -278,7 +278,7 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - [数组中数字出现的次数](src/array/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof.js) - - LeetCode 面试题56 - I. 数组中数字出现的次数 + - LeetCode 面试题 56 - I. 数组中数字出现的次数 - [只出现一次的数字](src/array/single-number.js) @@ -298,10 +298,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - [最大子序和](src/array/maximum-subarray.js) - LeetCode 53. 最大子序和 - - LeetCode 面试题42. 连续子数组的最大和 + - LeetCode 面试题 42. 连续子数组的最大和 - LintCode 41. 最大子数组 -- [跳跃游戏II](src/array/jump-game-ii.js) +- [跳跃游戏 II](src/array/jump-game-ii.js) - LeetCode 45. 跳跃游戏 II - LintCode 117. 跳跃游戏 II @@ -325,7 +325,7 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - [第一个只出现一次的字符](src/array/first-unique-character-in-a-string.js) - - LeetCode 面试题50. 第一个只出现一次的字符 + - LeetCode 面试题 50. 第一个只出现一次的字符 - LeetCode 387. 字符串中的第一个唯一字符 - LintCode 209. 第一个只出现一次的字符 @@ -334,26 +334,26 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 152. 乘积最大子数组 - LintCode 191. 乘积最大子序列 -- [最高频率的IP](src/array/highest-frequency-ip.js) +- [最高频率的 IP](src/array/highest-frequency-ip.js) - - LintCode 1613. 最高频率的IP + - LintCode 1613. 最高频率的 IP - [柠檬水找零](src/array/lemonade-change.js) - LeetCode 860. 柠檬水找零 - LintCode 1509. 柠檬水找零 -- [LRU缓存机制](src/array/lru-cache.js) +- [LRU 缓存机制](src/array/lru-cache.js) - - LeetCode 146. LRU缓存机制 - - LintCode 134. LRU缓存策略 + - LeetCode 146. LRU 缓存机制 + - LintCode 134. LRU 缓存策略 - [寻找重复数](src/array/find-the-duplicate-number.js) - LeetCode 287. 寻找重复数 - LintCode 633. 寻找重复的数 -- [和可被K整除的子数组](src/array/subarray-sums-divisible-by-k.js) +- [和可被 K 整除的子数组](src/array/subarray-sums-divisible-by-k.js) - LeetCode 974. 和可被 K 整除的子数组 @@ -381,10 +381,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 718. 最长重复子数组 - LintCode 79. 最长公共子串 -- [有序矩阵中第K小的元素](src/array/kth-smallest-element-in-a-sorted-matrix.js) +- [有序矩阵中第 K 小的元素](src/array/kth-smallest-element-in-a-sorted-matrix.js) - - LeetCode 378. 有序矩阵中第K小的元素 - - LintCode 401. 排序矩阵中的从小到大第k个数 + - LeetCode 378. 有序矩阵中第 K 小的元素 + - LintCode 401. 排序矩阵中的从小到大第 k 个数 ## 栈 @@ -440,12 +440,12 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - [Pow(x, n)](src/math/powx-n.js) - LeetCode 50. Pow(x, n) - - LintCode 428. x的n次幂 + - LintCode 428. x 的 n 次幂 -- [和为K的子数组](src/math/subarray-sum-equals-k.js) +- [和为 K 的子数组](src/math/subarray-sum-equals-k.js) - - LeetCode 560. 和为K的子数组 - - LintCode 838. 子数组和为K + - LeetCode 560. 和为 K 的子数组 + - LintCode 838. 子数组和为 K - [完美数](src/math/perfect-number.js) @@ -511,17 +511,22 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 105. 从前序与中序遍历序列构造二叉树 - LintCode 73. 前序遍历和中序遍历树构造二叉树 +- [将有序数组转换为二叉搜索树](src/tree/convert-sorted-array-to-binary-search-tree.js) + + - LeetCode 108. 将有序数组转换为二叉搜索树 + - LintCode 106. 有序链表转换为二叉搜索树 + ## 链表 -- [合并K个排序链表](src/list/merge-k-sorted-lists.js) +- [合并 K 个排序链表](src/list/merge-k-sorted-lists.js) - - LeetCode 23. 合并K个排序链表 - - LintCode 104. 合并k个排序链表 + - LeetCode 23. 合并 K 个排序链表 + - LintCode 104. 合并 k 个排序链表 - [合并两个有序链表](src/list/merge-two-sorted-lists.js) - LeetCode 21. 合并两个有序链表 - - LeetCode 面试题25. 合并两个排序的链表 + - LeetCode 面试题 25. 合并两个排序的链表 - LintCode 165. 合并两个排序链表 - [链表排序](src/list/sort-list.js) @@ -539,10 +544,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 876. 链表的中间结点 - LintCode 1609. 链表的中间结点 -- [K个一组翻转链表](src/list/reverse-nodes-in-k-group.js) +- [K 个一组翻转链表](src/list/reverse-nodes-in-k-group.js) - LeetCode 25. K 个一组翻转链表 - - LintCode 450. K组翻转链表 + - LintCode 450. K 组翻转链表 ## 图 diff --git a/src/tree/convert-sorted-array-to-binary-search-tree.js b/src/tree/convert-sorted-array-to-binary-search-tree.js new file mode 100644 index 0000000..0705a8a --- /dev/null +++ b/src/tree/convert-sorted-array-to-binary-search-tree.js @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + */ +function TreeNode(val) { + this.val = val + this.left = this.right = null +} + +/** + * @param {number[]} nums + * @return {TreeNode} + */ +export const sortedArrayToBST = function (nums) { + if (!nums.length) return null + + const creatTree = (left, right) => { + if (left > right) return null + const mid = Math.floor((left + right) / 2) + const root = new TreeNode(nums[mid]) + root.left = creatTree(left, mid - 1) + root.right = creatTree(mid + 1, right) + return root + } + + return creatTree(0, nums.length - 1) +} diff --git a/test/tree/convert-sorted-array-to-binary-search-tree.test.js b/test/tree/convert-sorted-array-to-binary-search-tree.test.js new file mode 100644 index 0000000..4153faa --- /dev/null +++ b/test/tree/convert-sorted-array-to-binary-search-tree.test.js @@ -0,0 +1,5 @@ +import { sortedArrayToBST } from '../../src/tree/convert-sorted-array-to-binary-search-tree.js' + +test('将有序数组转换为二叉搜索树', () => { + expect(sortedArrayToBST()).toEqual() +})