File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 103
103
101|[ Symmetric Tree] ( ./0101-symmetric-tree.js ) |Easy|
104
104
102|[ Binary Tree Level Order Traversal] ( ./0102-binary-tree-level-order-traversal.js ) |Medium|
105
105
104|[ Maximum Depth of Binary Tree] ( ./0104-maximum-depth-of-binary-tree.js ) |Easy|
106
+ 108|[ Convert Sorted Array to Binary Search Tree] ( ./0108-convert-sorted-array-to-binary-search-tree.js ) |Easy|
106
107
110|[ Balanced Binary Tree] ( ./0110-balanced-binary-tree.js ) |Easy|
107
108
111|[ Minimum Depth of Binary Tree] ( ./0111-minimum-depth-of-binary-tree.js ) |Easy|
108
109
112|[ Path Sum] ( ./0112-path-sum.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 108. Convert Sorted Array to Binary Search Tree
3
+ * https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given an integer array nums where the elements are sorted in ascending order, convert it
7
+ * to a height-balanced binary search tree.
8
+ */
9
+
10
+ /**
11
+ * Definition for a binary tree node.
12
+ * function TreeNode(val, left, right) {
13
+ * this.val = (val===undefined ? 0 : val)
14
+ * this.left = (left===undefined ? null : left)
15
+ * this.right = (right===undefined ? null : right)
16
+ * }
17
+ */
18
+ /**
19
+ * @param {number[] } nums
20
+ * @return {TreeNode }
21
+ */
22
+ var sortedArrayToBST = function ( nums ) {
23
+ if ( ! nums . length ) return null ;
24
+
25
+ const middleIndex = Math . floor ( nums . length / 2 ) ;
26
+ const root = new TreeNode ( nums [ middleIndex ] ) ;
27
+ root . left = sortedArrayToBST ( nums . slice ( 0 , middleIndex ) ) ;
28
+ root . right = sortedArrayToBST ( nums . slice ( middleIndex + 1 ) ) ;
29
+
30
+ return root ;
31
+ } ;
You can’t perform that action at this time.
0 commit comments