Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1690ae7

Browse files
committed
22w7: add 108 js solution
1 parent 594ef31 commit 1690ae7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@
335335
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| [js](./algorithms/minimumDepthOfBinaryTree/minimumDepthOfBinaryTree.js) |Easy|
336336
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)| [java](./algorithms/balancedBinaryTree/Solution.java), [js](./algorithms/balancedBinaryTree/Solution.js) |Easy|
337337
|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)| |Medium|
338-
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| [Java](./algorithms/convertSortedArrayToBST/Solution.java) |Medium|
338+
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| [Java](./algorithms/convertSortedArrayToBST/Solution.java), [js](./algorithms/convertSortedArrayToBST/solution.js) |Medium|
339339
|107|[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/)| |Easy|
340340
|106|[Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| |Medium|
341341
|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| |Medium|
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {number[]} nums
11+
* @return {TreeNode}
12+
*/
13+
var sortedArrayToBST = function(nums) {
14+
if (!nums.length) return null;
15+
// 取中间的索引
16+
const middle = nums.length >> 1;
17+
return new TreeNode(
18+
nums[middle], // 根节点为中间节点
19+
sortedArrayToBST(nums.slice(0, middle)), // 左子树是左侧值
20+
sortedArrayToBST(nums.slice(middle + 1)), // 右子树为右侧值
21+
)
22+
};

0 commit comments

Comments
 (0)