61
61
62
62
<!-- 这里可写通用的实现逻辑 -->
63
63
64
+ 二叉搜索树的中序遍历(左根右)结果是一个单调递增的有序序列,我们反序进行中序遍历(右根左),即可以得到一个单调递减的有序序列。通过累加单调递减的有序序列,我们可以得到大于等于 node.val 的新值,并重新赋值给 node。
65
+
66
+ 关于反序中序遍历,有三种方法,一是递归遍历,二是栈实现非递归遍历,三是 Morris 遍历。
67
+
64
68
<!-- tabs:start -->
65
69
66
70
### ** Python3**
67
71
68
72
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
73
74
+ 递归遍历:
75
+
70
76
``` python
71
77
# Definition for a binary tree node.
72
78
# class TreeNode:
@@ -85,10 +91,45 @@ class Solution:
85
91
return root
86
92
```
87
93
94
+ Morris 遍历:
95
+
96
+ ``` python
97
+ # Definition for a binary tree node.
98
+ # class TreeNode:
99
+ # def __init__(self, val=0, left=None, right=None):
100
+ # self.val = val
101
+ # self.left = left
102
+ # self.right = right
103
+ class Solution :
104
+ def convertBST (self , root : TreeNode) -> TreeNode:
105
+ s = 0
106
+ node = root
107
+ while root:
108
+ if root.right is None :
109
+ s += root.val
110
+ root.val = s
111
+ root = root.left
112
+ else :
113
+ next = root.right
114
+ while next .left and next .left != root:
115
+ next = next .left
116
+ if next .left is None :
117
+ next .left = root
118
+ root = root.right
119
+ else :
120
+ s += root.val
121
+ root.val = s
122
+ next .left = None
123
+ root = root.left
124
+ return node
125
+ ```
126
+
88
127
### ** Java**
89
128
90
129
<!-- 这里可写当前语言的特殊实现逻辑 -->
91
130
131
+ 递归遍历:
132
+
92
133
``` java
93
134
class Solution {
94
135
int add = 0 ;
@@ -104,8 +145,58 @@ class Solution {
104
145
}
105
146
```
106
147
148
+ Morris 遍历:
149
+
150
+ ``` java
151
+ /**
152
+ * Definition for a binary tree node.
153
+ * public class TreeNode {
154
+ * int val;
155
+ * TreeNode left;
156
+ * TreeNode right;
157
+ * TreeNode() {}
158
+ * TreeNode(int val) { this.val = val; }
159
+ * TreeNode(int val, TreeNode left, TreeNode right) {
160
+ * this.val = val;
161
+ * this.left = left;
162
+ * this.right = right;
163
+ * }
164
+ * }
165
+ */
166
+ class Solution {
167
+ public TreeNode convertBST (TreeNode root ) {
168
+ int s = 0 ;
169
+ TreeNode node = root;
170
+ while (root != null ) {
171
+ if (root. right == null ) {
172
+ s += root. val;
173
+ root. val = s;
174
+ root = root. left;
175
+ } else {
176
+ TreeNode next = root. right;
177
+ while (next. left != null && next. left != root) {
178
+ next = next. left;
179
+ }
180
+ if (next. left == null ) {
181
+ next. left = root;
182
+ root = root. right;
183
+ } else {
184
+ s += root. val;
185
+ root. val = s;
186
+ next. left = null ;
187
+ root = root. left;
188
+ }
189
+ }
190
+ }
191
+ return node;
192
+ }
193
+ }
194
+ ```
195
+
107
196
### ** C++**
108
197
198
+ 递归遍历:
199
+
109
200
``` cpp
110
201
/* *
111
202
* Definition for a binary tree node.
@@ -133,6 +224,100 @@ public:
133
224
};
134
225
```
135
226
227
+ Morris 遍历:
228
+
229
+ ```cpp
230
+ /**
231
+ * Definition for a binary tree node.
232
+ * struct TreeNode {
233
+ * int val;
234
+ * TreeNode *left;
235
+ * TreeNode *right;
236
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
237
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
238
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
239
+ * };
240
+ */
241
+ class Solution {
242
+ public:
243
+ TreeNode *convertBST(TreeNode *root) {
244
+ int s = 0;
245
+ TreeNode *node = root;
246
+ while (root)
247
+ {
248
+ if (root->right == nullptr)
249
+ {
250
+ s += root->val;
251
+ root->val = s;
252
+ root = root->left;
253
+ }
254
+ else
255
+ {
256
+ TreeNode *next = root->right;
257
+ while (next->left && next->left != root)
258
+ {
259
+ next = next->left;
260
+ }
261
+ if (next->left == nullptr)
262
+ {
263
+ next->left = root;
264
+ root = root->right;
265
+ }
266
+ else
267
+ {
268
+ s += root->val;
269
+ root->val = s;
270
+ next->left = nullptr;
271
+ root = root->left;
272
+ }
273
+ }
274
+ }
275
+ return node;
276
+ }
277
+ };
278
+ ```
279
+
280
+ ### ** Go**
281
+
282
+ Morris 遍历:
283
+
284
+ ``` go
285
+ /* *
286
+ * Definition for a binary tree node.
287
+ * type TreeNode struct {
288
+ * Val int
289
+ * Left *TreeNode
290
+ * Right *TreeNode
291
+ * }
292
+ */
293
+ func convertBST (root *TreeNode ) *TreeNode {
294
+ s := 0
295
+ node := root
296
+ for root != nil {
297
+ if root.Right == nil {
298
+ s += root.Val
299
+ root.Val = s
300
+ root = root.Left
301
+ } else {
302
+ next := root.Right
303
+ for next.Left != nil && next.Left != root {
304
+ next = next.Left
305
+ }
306
+ if next.Left == nil {
307
+ next.Left = root
308
+ root = root.Right
309
+ } else {
310
+ s += root.Val
311
+ root.Val = s
312
+ next.Left = nil
313
+ root = root.Left
314
+ }
315
+ }
316
+ }
317
+ return node
318
+ }
319
+ ```
320
+
136
321
### ** ...**
137
322
138
323
```
0 commit comments