Skip to content

Commit

Permalink
Merge branch 'youngyangyang04:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
SianXiaoCHN authored Apr 4, 2022
2 parents 69a9316 + 5c3ab04 commit 45cc756
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion problems/0134.加油站.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public:
```
* 时间复杂度:$O(n^2)$
* 空间复杂度:$O(n)$
* 空间复杂度:$O(1)$
C++暴力解法在leetcode上提交也可以过。
Expand Down
42 changes: 42 additions & 0 deletions problems/0538.把二叉搜索树转换为累加树.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,47 @@ struct TreeNode* convertBST(struct TreeNode* root){
}
```
## TypeScript
> 递归法
```typescript
function convertBST(root: TreeNode | null): TreeNode | null {
let pre: number = 0;
function recur(root: TreeNode | null): void {
if (root === null) return;
recur(root.right);
root.val += pre;
pre = root.val;
recur(root.left);
}
recur(root);
return root;
};
```

> 迭代法
```typescript
function convertBST(root: TreeNode | null): TreeNode | null {
const helperStack: TreeNode[] = [];
let curNode: TreeNode | null = root;
let pre: number = 0;
while (curNode !== null || helperStack.length > 0) {
while (curNode !== null) {
helperStack.push(curNode);
curNode = curNode.right;
}
curNode = helperStack.pop()!;
curNode.val += pre;
pre = curNode.val;
curNode = curNode.left;
}
return root;
};
```



-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 comments on commit 45cc756

Please sign in to comment.