Skip to content

Commit 5b7ed8d

Browse files
Merge pull request youngyangyang04#1158 from xiaofei-2020/tree33
添加(0108.将有序数组转换为二叉搜索树.md):增加typescript版本
2 parents 26caa33 + e03a512 commit 5b7ed8d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

problems/0108.将有序数组转换为二叉搜索树.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class Solution {
251251
return root;
252252
}
253253
}
254-
```
254+
```
255255

256256
迭代: 左闭右闭 [left,right]
257257
```java
@@ -373,7 +373,24 @@ var sortedArrayToBST = function (nums) {
373373
};
374374
```
375375

376+
## TypeScript
377+
378+
```typescript
379+
function sortedArrayToBST(nums: number[]): TreeNode | null {
380+
function recur(nums: number[], left: number, right: number): TreeNode | null {
381+
if (left > right) return null;
382+
let mid: number = Math.floor((left + right) / 2);
383+
const root: TreeNode = new TreeNode(nums[mid]);
384+
root.left = recur(nums, left, mid - 1);
385+
root.right = recur(nums, mid + 1, right);
386+
return root;
387+
}
388+
return recur(nums, 0, nums.length - 1);
389+
};
390+
```
391+
376392
## C
393+
377394
递归
378395
```c
379396
struct TreeNode* traversal(int* nums, int left, int right) {

0 commit comments

Comments
 (0)