mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -373,7 +373,24 @@ var sortedArrayToBST = function (nums) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## TypeScript
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function sortedArrayToBST(nums: number[]): TreeNode | null {
|
||||||
|
function recur(nums: number[], left: number, right: number): TreeNode | null {
|
||||||
|
if (left > right) return null;
|
||||||
|
let mid: number = Math.floor((left + right) / 2);
|
||||||
|
const root: TreeNode = new TreeNode(nums[mid]);
|
||||||
|
root.left = recur(nums, left, mid - 1);
|
||||||
|
root.right = recur(nums, mid + 1, right);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
return recur(nums, 0, nums.length - 1);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
## C
|
## C
|
||||||
|
|
||||||
递归
|
递归
|
||||||
```c
|
```c
|
||||||
struct TreeNode* traversal(int* nums, int left, int right) {
|
struct TreeNode* traversal(int* nums, int left, int right) {
|
||||||
|
@ -405,6 +405,56 @@ var trimBST = function (root,low,high) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## TypeScript
|
||||||
|
|
||||||
|
> 递归法
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | null {
|
||||||
|
if (root === null) return null;
|
||||||
|
if (root.val < low) {
|
||||||
|
return trimBST(root.right, low, high);
|
||||||
|
}
|
||||||
|
if (root.val > high) {
|
||||||
|
return trimBST(root.left, low, high);
|
||||||
|
}
|
||||||
|
root.left = trimBST(root.left, low, high);
|
||||||
|
root.right = trimBST(root.right, low, high);
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
> 迭代法
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | null {
|
||||||
|
while (root !== null && (root.val < low || root.val > high)) {
|
||||||
|
if (root.val < low) {
|
||||||
|
root = root.right;
|
||||||
|
} else if (root.val > high) {
|
||||||
|
root = root.left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let curNode: TreeNode | null = root;
|
||||||
|
while (curNode !== null) {
|
||||||
|
while (curNode.left !== null && curNode.left.val < low) {
|
||||||
|
curNode.left = curNode.left.right;
|
||||||
|
}
|
||||||
|
curNode = curNode.left;
|
||||||
|
}
|
||||||
|
curNode = root;
|
||||||
|
while (curNode !== null) {
|
||||||
|
while (curNode.right !== null && curNode.right.val > high) {
|
||||||
|
curNode.right = curNode.right.left;
|
||||||
|
}
|
||||||
|
curNode = curNode.right;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user