72
72
73
73
<!-- 这里可写通用的实现逻辑 -->
74
74
75
- 二叉搜索树的中序遍历(左根右)结果是一个单调递增的有序序列,我们反序进行中序遍历(右根左),即可以得到一个单调递减的有序序列。通过累加单调递减的有序序列,我们可以得到大于等于 node.val 的新值,并重新赋值给 node。
75
+ ** 前言**
76
+
77
+ 二叉搜索树的中序遍历(左根右)结果是一个单调递增的有序序列,我们反序进行中序遍历(右根左),即可以得到一个单调递减的有序序列。通过累加单调递减的有序序列,我们可以得到大于等于 ` node.val ` 的新值,并重新赋值给 ` node ` 。
76
78
77
79
关于反序中序遍历,有三种方法,一是递归遍历,二是栈实现非递归遍历,三是 Morris 遍历。
78
80
79
- Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
81
+ ** 方法一:递归**
82
+
83
+ 按照“右根左”的顺序,递归遍历二叉搜索树,累加遍历到的所有节点值到 $s$ 中,然后每次赋值给对应的 ` node ` 节点。
84
+
85
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉搜索树的节点数。
86
+
87
+ ** 方法二:Morris 遍历**
80
88
81
- 定义 s 表示二叉搜索树节点值累加之和。遍历二叉树节点,
89
+ Morris 遍历无需使用栈,时间复杂度 $O(n)$,空间复杂度为 $O(1)$。核心思想是:
90
+
91
+ 定义 s 表示二叉搜索树节点值累加和。遍历二叉树节点:
82
92
83
93
1 . 若当前节点 root 的右子树为空,** 将当前节点值添加至 s** 中,更新当前节点值为 s,并将当前节点更新为 ` root.left ` 。
84
94
2 . 若当前节点 root 的右子树不为空,找到右子树的最左节点 next(也即是 root 节点在中序遍历下的后继节点):
@@ -105,14 +115,18 @@ Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
105
115
# self.left = left
106
116
# self.right = right
107
117
class Solution :
108
- add = 0
109
-
110
118
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)
116
130
return root
117
131
```
118
132
@@ -156,17 +170,38 @@ class Solution:
156
170
递归遍历:
157
171
158
172
``` 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
+ */
159
188
class Solution {
160
- int add = 0 ;
189
+ private int s;
190
+
161
191
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);
168
193
return root;
169
194
}
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
+ }
170
205
}
171
206
```
172
207
@@ -236,16 +271,20 @@ class Solution {
236
271
*/
237
272
class Solution {
238
273
public:
239
- int add = 0;
274
+ int s = 0;
275
+
240
276
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);
247
278
return root;
248
279
}
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
+ }
249
288
};
250
289
```
251
290
@@ -296,6 +335,34 @@ public:
296
335
297
336
### ** Go**
298
337
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
+
299
366
Morris 遍历:
300
367
301
368
``` go
@@ -335,6 +402,37 @@ func convertBST(root *TreeNode) *TreeNode {
335
402
}
336
403
```
337
404
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
+
338
436
### ** ...**
339
437
340
438
```
0 commit comments