Skip to content

Commit 75edf4c

Browse files
committed
feat: add solutions to lc problems
* No.0538.Convert BST to Greater Tree * No.1038.Binary Search Tree to Greater Sum Tree
1 parent c2a2117 commit 75edf4c

File tree

8 files changed

+571
-143
lines changed

8 files changed

+571
-143
lines changed

lcof2/剑指 Offer II 054. 所有大于等于节点的值之和/README.md

Lines changed: 122 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,23 @@
7272

7373
<!-- 这里可写通用的实现逻辑 -->
7474

75-
二叉搜索树的中序遍历(左根右)结果是一个单调递增的有序序列,我们反序进行中序遍历(右根左),即可以得到一个单调递减的有序序列。通过累加单调递减的有序序列,我们可以得到大于等于 node.val 的新值,并重新赋值给 node。
75+
**前言**
76+
77+
二叉搜索树的中序遍历(左根右)结果是一个单调递增的有序序列,我们反序进行中序遍历(右根左),即可以得到一个单调递减的有序序列。通过累加单调递减的有序序列,我们可以得到大于等于 `node.val` 的新值,并重新赋值给 `node`
7678

7779
关于反序中序遍历,有三种方法,一是递归遍历,二是栈实现非递归遍历,三是 Morris 遍历。
7880

79-
Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
81+
**方法一:递归**
82+
83+
按照“右根左”的顺序,递归遍历二叉搜索树,累加遍历到的所有节点值到 $s$ 中,然后每次赋值给对应的 `node` 节点。
84+
85+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉搜索树的节点数。
86+
87+
**方法二:Morris 遍历**
8088

81-
定义 s 表示二叉搜索树节点值累加之和。遍历二叉树节点,
89+
Morris 遍历无需使用栈,时间复杂度 $O(n)$,空间复杂度为 $O(1)$。核心思想是:
90+
91+
定义 s 表示二叉搜索树节点值累加和。遍历二叉树节点:
8292

8393
1. 若当前节点 root 的右子树为空,**将当前节点值添加至 s** 中,更新当前节点值为 s,并将当前节点更新为 `root.left`
8494
2. 若当前节点 root 的右子树不为空,找到右子树的最左节点 next(也即是 root 节点在中序遍历下的后继节点):
@@ -105,14 +115,18 @@ Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
105115
# self.left = left
106116
# self.right = right
107117
class Solution:
108-
add = 0
109-
110118
def convertBST(self, root: TreeNode) -> TreeNode:
111-
if root:
112-
self.convertBST(root.right)
113-
root.val += self.add
114-
self.add = root.val
115-
self.convertBST(root.left)
119+
def dfs(root):
120+
nonlocal s
121+
if root is None:
122+
return
123+
dfs(root.right)
124+
s += root.val
125+
root.val = s
126+
dfs(root.left)
127+
128+
s = 0
129+
dfs(root)
116130
return root
117131
```
118132

@@ -156,17 +170,38 @@ class Solution:
156170
递归遍历:
157171

158172
```java
173+
/**
174+
* Definition for a binary tree node.
175+
* public class TreeNode {
176+
* int val;
177+
* TreeNode left;
178+
* TreeNode right;
179+
* TreeNode() {}
180+
* TreeNode(int val) { this.val = val; }
181+
* TreeNode(int val, TreeNode left, TreeNode right) {
182+
* this.val = val;
183+
* this.left = left;
184+
* this.right = right;
185+
* }
186+
* }
187+
*/
159188
class Solution {
160-
int add = 0;
189+
private int s;
190+
161191
public TreeNode convertBST(TreeNode root) {
162-
if (root != null) {
163-
convertBST(root.right);
164-
root.val += add;
165-
add = root.val;
166-
convertBST(root.left);
167-
}
192+
dfs(root);
168193
return root;
169194
}
195+
196+
private void dfs(TreeNode root) {
197+
if (root == null) {
198+
return;
199+
}
200+
dfs(root.right);
201+
s += root.val;
202+
root.val = s;
203+
dfs(root.left);
204+
}
170205
}
171206
```
172207

@@ -236,16 +271,20 @@ class Solution {
236271
*/
237272
class Solution {
238273
public:
239-
int add = 0;
274+
int s = 0;
275+
240276
TreeNode* convertBST(TreeNode* root) {
241-
if (root) {
242-
convertBST(root->right);
243-
root->val += add;
244-
add = root->val;
245-
convertBST(root->left);
246-
}
277+
dfs(root);
247278
return root;
248279
}
280+
281+
void dfs(TreeNode* root) {
282+
if (!root) return;
283+
dfs(root->right);
284+
s += root->val;
285+
root->val = s;
286+
dfs(root->left);
287+
}
249288
};
250289
```
251290
@@ -296,6 +335,34 @@ public:
296335

297336
### **Go**
298337

338+
递归遍历:
339+
340+
```go
341+
/**
342+
* Definition for a binary tree node.
343+
* type TreeNode struct {
344+
* Val int
345+
* Left *TreeNode
346+
* Right *TreeNode
347+
* }
348+
*/
349+
func convertBST(root *TreeNode) *TreeNode {
350+
s := 0
351+
var dfs func(*TreeNode)
352+
dfs = func(root *TreeNode) {
353+
if root == nil {
354+
return
355+
}
356+
dfs(root.Right)
357+
s += root.Val
358+
root.Val = s
359+
dfs(root.Left)
360+
}
361+
dfs(root)
362+
return root
363+
}
364+
```
365+
299366
Morris 遍历:
300367

301368
```go
@@ -335,6 +402,37 @@ func convertBST(root *TreeNode) *TreeNode {
335402
}
336403
```
337404

405+
### **JavaScript**
406+
407+
```js
408+
/**
409+
* Definition for a binary tree node.
410+
* function TreeNode(val, left, right) {
411+
* this.val = (val===undefined ? 0 : val)
412+
* this.left = (left===undefined ? null : left)
413+
* this.right = (right===undefined ? null : right)
414+
* }
415+
*/
416+
/**
417+
* @param {TreeNode} root
418+
* @return {TreeNode}
419+
*/
420+
var convertBST = function (root) {
421+
let s = 0;
422+
function dfs(root) {
423+
if (!root) {
424+
return;
425+
}
426+
dfs(root.right);
427+
s += root.val;
428+
root.val = s;
429+
dfs(root.left);
430+
}
431+
dfs(root);
432+
return root;
433+
};
434+
```
435+
338436
### **...**
339437

340438
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 {TreeNode} root
11+
* @return {TreeNode}
12+
*/
13+
var convertBST = function (root) {
14+
let s = 0;
15+
function dfs(root) {
16+
if (!root) {
17+
return;
18+
}
19+
dfs(root.right);
20+
s += root.val;
21+
root.val = s;
22+
dfs(root.left);
23+
}
24+
dfs(root);
25+
return root;
26+
};

0 commit comments

Comments
 (0)