|
68 | 68 | <li>本题与主站 1038 题相同:<a href="https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/">https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/</a></li>
|
69 | 69 | </ul>
|
70 | 70 |
|
71 |
| - |
72 | 71 | ## 解法
|
73 | 72 |
|
74 | 73 | <!-- 这里可写通用的实现逻辑 -->
|
75 | 74 |
|
| 75 | +二叉搜索树的中序遍历(左根右)结果是一个单调递增的有序序列,我们反序进行中序遍历(右根左),即可以得到一个单调递减的有序序列。通过累加单调递减的有序序列,我们可以得到大于等于 node.val 的新值,并重新赋值给 node。 |
| 76 | + |
| 77 | +关于反序中序遍历,有三种方法,一是递归遍历,二是栈实现非递归遍历,三是 Morris 遍历。 |
| 78 | + |
| 79 | +Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是: |
| 80 | + |
| 81 | +定义 s 表示二叉搜索树节点值累加之和。遍历二叉树节点, |
| 82 | + |
| 83 | +1. 若当前节点 root 的右子树为空,**将当前节点值添加至 s** 中,更新当前节点值为 s,并将当前节点更新为 `root.left`。 |
| 84 | +2. 若当前节点 root 的右子树不为空,找到右子树的最左节点 next(也即是 root 节点在中序遍历下的后继节点): |
| 85 | + - 若后继节点 next 的左子树为空,将后继节点的左子树指向当前节点 root,并将当前节点更新为 `root.right`。 |
| 86 | + - 若后继节点 next 的左子树不为空,**将当前节点值添加 s** 中,更新当前节点值为 s,然后将后继节点左子树指向空(即解除 next 与 root 的指向关系),并将当前节点更新为 `root.left`。 |
| 87 | +3. 循环以上步骤,直至二叉树节点为空,遍历结束。 |
| 88 | +4. 最后返回二叉搜索树根节点即可。 |
| 89 | + |
| 90 | +> Morris 反序中序遍历跟 Morris 中序遍历思路一致,只是将中序遍历的“左根右”变为“右根左”。 |
| 91 | +
|
76 | 92 | <!-- tabs:start -->
|
77 | 93 |
|
78 | 94 | ### **Python3**
|
79 | 95 |
|
80 | 96 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
81 | 97 |
|
| 98 | +递归遍历: |
| 99 | + |
82 | 100 | ```python
|
| 101 | +# Definition for a binary tree node. |
| 102 | +# class TreeNode: |
| 103 | +# def __init__(self, val=0, left=None, right=None): |
| 104 | +# self.val = val |
| 105 | +# self.left = left |
| 106 | +# self.right = right |
| 107 | +class Solution: |
| 108 | + add = 0 |
| 109 | + def convertBST(self, root: TreeNode) -> TreeNode: |
| 110 | + if root: |
| 111 | + self.convertBST(root.right) |
| 112 | + root.val += self.add |
| 113 | + self.add = root.val |
| 114 | + self.convertBST(root.left) |
| 115 | + return root |
| 116 | +``` |
83 | 117 |
|
| 118 | +Morris 遍历: |
| 119 | + |
| 120 | +```python |
| 121 | +# Definition for a binary tree node. |
| 122 | +# class TreeNode: |
| 123 | +# def __init__(self, val=0, left=None, right=None): |
| 124 | +# self.val = val |
| 125 | +# self.left = left |
| 126 | +# self.right = right |
| 127 | +class Solution: |
| 128 | + def convertBST(self, root: TreeNode) -> TreeNode: |
| 129 | + s = 0 |
| 130 | + node = root |
| 131 | + while root: |
| 132 | + if root.right is None: |
| 133 | + s += root.val |
| 134 | + root.val = s |
| 135 | + root = root.left |
| 136 | + else: |
| 137 | + next = root.right |
| 138 | + while next.left and next.left != root: |
| 139 | + next = next.left |
| 140 | + if next.left is None: |
| 141 | + next.left = root |
| 142 | + root = root.right |
| 143 | + else: |
| 144 | + s += root.val |
| 145 | + root.val = s |
| 146 | + next.left = None |
| 147 | + root = root.left |
| 148 | + return node |
84 | 149 | ```
|
85 | 150 |
|
86 | 151 | ### **Java**
|
87 | 152 |
|
88 | 153 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
89 | 154 |
|
| 155 | +递归遍历: |
| 156 | + |
| 157 | +```java |
| 158 | +class Solution { |
| 159 | + int add = 0; |
| 160 | + public TreeNode convertBST(TreeNode root) { |
| 161 | + if (root != null) { |
| 162 | + convertBST(root.right); |
| 163 | + root.val += add; |
| 164 | + add = root.val; |
| 165 | + convertBST(root.left); |
| 166 | + } |
| 167 | + return root; |
| 168 | + } |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +Morris 遍历: |
| 173 | + |
90 | 174 | ```java
|
| 175 | +/** |
| 176 | + * Definition for a binary tree node. |
| 177 | + * public class TreeNode { |
| 178 | + * int val; |
| 179 | + * TreeNode left; |
| 180 | + * TreeNode right; |
| 181 | + * TreeNode() {} |
| 182 | + * TreeNode(int val) { this.val = val; } |
| 183 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 184 | + * this.val = val; |
| 185 | + * this.left = left; |
| 186 | + * this.right = right; |
| 187 | + * } |
| 188 | + * } |
| 189 | + */ |
| 190 | +class Solution { |
| 191 | + public TreeNode convertBST(TreeNode root) { |
| 192 | + int s = 0; |
| 193 | + TreeNode node = root; |
| 194 | + while (root != null) { |
| 195 | + if (root.right == null) { |
| 196 | + s += root.val; |
| 197 | + root.val = s; |
| 198 | + root = root.left; |
| 199 | + } else { |
| 200 | + TreeNode next = root.right; |
| 201 | + while (next.left != null && next.left != root) { |
| 202 | + next = next.left; |
| 203 | + } |
| 204 | + if (next.left == null) { |
| 205 | + next.left = root; |
| 206 | + root = root.right; |
| 207 | + } else { |
| 208 | + s += root.val; |
| 209 | + root.val = s; |
| 210 | + next.left = null; |
| 211 | + root = root.left; |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + return node; |
| 216 | + } |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +### **C++** |
| 221 | + |
| 222 | +递归遍历: |
| 223 | + |
| 224 | +```cpp |
| 225 | +/** |
| 226 | + * Definition for a binary tree node. |
| 227 | + * struct TreeNode { |
| 228 | + * int val; |
| 229 | + * TreeNode *left; |
| 230 | + * TreeNode *right; |
| 231 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 232 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 233 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 234 | + * }; |
| 235 | + */ |
| 236 | +class Solution { |
| 237 | +public: |
| 238 | + int add = 0; |
| 239 | + TreeNode* convertBST(TreeNode* root) { |
| 240 | + if (root) { |
| 241 | + convertBST(root->right); |
| 242 | + root->val += add; |
| 243 | + add = root->val; |
| 244 | + convertBST(root->left); |
| 245 | + } |
| 246 | + return root; |
| 247 | + } |
| 248 | +}; |
| 249 | +``` |
| 250 | +
|
| 251 | +Morris 遍历: |
| 252 | +
|
| 253 | +```cpp |
| 254 | +/** |
| 255 | + * Definition for a binary tree node. |
| 256 | + * struct TreeNode { |
| 257 | + * int val; |
| 258 | + * TreeNode *left; |
| 259 | + * TreeNode *right; |
| 260 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 261 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 262 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 263 | + * }; |
| 264 | + */ |
| 265 | +class Solution { |
| 266 | +public: |
| 267 | + TreeNode *convertBST(TreeNode *root) { |
| 268 | + int s = 0; |
| 269 | + TreeNode *node = root; |
| 270 | + while (root) |
| 271 | + { |
| 272 | + if (root->right == nullptr) |
| 273 | + { |
| 274 | + s += root->val; |
| 275 | + root->val = s; |
| 276 | + root = root->left; |
| 277 | + } |
| 278 | + else |
| 279 | + { |
| 280 | + TreeNode *next = root->right; |
| 281 | + while (next->left && next->left != root) |
| 282 | + { |
| 283 | + next = next->left; |
| 284 | + } |
| 285 | + if (next->left == nullptr) |
| 286 | + { |
| 287 | + next->left = root; |
| 288 | + root = root->right; |
| 289 | + } |
| 290 | + else |
| 291 | + { |
| 292 | + s += root->val; |
| 293 | + root->val = s; |
| 294 | + next->left = nullptr; |
| 295 | + root = root->left; |
| 296 | + } |
| 297 | + } |
| 298 | + } |
| 299 | + return node; |
| 300 | + } |
| 301 | +}; |
| 302 | +``` |
91 | 303 |
|
| 304 | +### **Go** |
| 305 | + |
| 306 | +Morris 遍历: |
| 307 | + |
| 308 | +```go |
| 309 | +/** |
| 310 | + * Definition for a binary tree node. |
| 311 | + * type TreeNode struct { |
| 312 | + * Val int |
| 313 | + * Left *TreeNode |
| 314 | + * Right *TreeNode |
| 315 | + * } |
| 316 | + */ |
| 317 | +func convertBST(root *TreeNode) *TreeNode { |
| 318 | + s := 0 |
| 319 | + node := root |
| 320 | + for root != nil { |
| 321 | + if root.Right == nil { |
| 322 | + s += root.Val |
| 323 | + root.Val = s |
| 324 | + root = root.Left |
| 325 | + } else { |
| 326 | + next := root.Right |
| 327 | + for next.Left != nil && next.Left != root { |
| 328 | + next = next.Left |
| 329 | + } |
| 330 | + if next.Left == nil { |
| 331 | + next.Left = root |
| 332 | + root = root.Right |
| 333 | + } else { |
| 334 | + s += root.Val |
| 335 | + root.Val = s |
| 336 | + next.Left = nil |
| 337 | + root = root.Left |
| 338 | + } |
| 339 | + } |
| 340 | + } |
| 341 | + return node |
| 342 | +} |
92 | 343 | ```
|
93 | 344 |
|
94 | 345 | ### **...**
|
|
0 commit comments