|
42 | 42 |
|
43 | 43 | <!-- 这里可写通用的实现逻辑 -->
|
44 | 44 |
|
| 45 | +**方法一:递归** |
| 46 | + |
| 47 | +判断 `root.val` 与 `low` 和 `high` 的大小关系: |
| 48 | + |
| 49 | +- 若 `root.val` 大于 `high`,说明当前 `root` 节点与其右子树所有节点的值均大于 `high`,那么递归修剪 `root.left` 即可; |
| 50 | +- 若 `root.val` 小于 `low`,说明当前 `root` 节点与其左子树所有节点的值均小于 `low`,那么递归修剪 `root.right` 即可; |
| 51 | +- 若 `root.val` 在 `[low, high]` 之间,说明当前 `root` 应该保留,递归修剪 `root.left`, `root.right`,并且返回 `root`。 |
| 52 | + |
| 53 | +递归的终止条件是 `root` 节点为空。 |
| 54 | + |
| 55 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉搜索树的节点个数。 |
| 56 | + |
| 57 | +**方法二:迭代** |
| 58 | + |
| 59 | +我们先循环判断 `root`,若 `root.val` 不在 `[low, high]` 之间,那么直接将 `root` 置为对应的左孩子或右孩子,循环直至 `root` 为空或者 `root.val` 在 `[low, high]` 之间。 |
| 60 | + |
| 61 | +若此时 `root` 为空,直接返回。否则,说明 `root` 是一个需要保留的节点。接下来只需要分别迭代修剪 `root` 的左右子树。 |
| 62 | + |
| 63 | +以左子树 `node = root.left` 为例: |
| 64 | + |
| 65 | +- 若 `node.left.val` 小于 `low`,那么 `node.left` 及其左孩子均不满足条件,我们直接将 `node.left` 置为 `node.left.right`; |
| 66 | +- 否则,我们将 `node` 置为 `node.left`; |
| 67 | +- 循环判断,直至 `node.left` 为空。 |
| 68 | + |
| 69 | +右子树的修剪过程与之类似。 |
| 70 | + |
| 71 | +时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是二叉搜索树的节点个数。 |
| 72 | + |
45 | 73 | <!-- tabs:start -->
|
46 | 74 |
|
47 | 75 | ### **Python3**
|
@@ -73,6 +101,36 @@ class Solution:
|
73 | 101 | return dfs(root)
|
74 | 102 | ```
|
75 | 103 |
|
| 104 | +```python |
| 105 | +# Definition for a binary tree node. |
| 106 | +# class TreeNode: |
| 107 | +# def __init__(self, val=0, left=None, right=None): |
| 108 | +# self.val = val |
| 109 | +# self.left = left |
| 110 | +# self.right = right |
| 111 | +class Solution: |
| 112 | + def trimBST( |
| 113 | + self, root: Optional[TreeNode], low: int, high: int |
| 114 | + ) -> Optional[TreeNode]: |
| 115 | + while root and (root.val < low or root.val > high): |
| 116 | + root = root.left if root.val > high else root.right |
| 117 | + if root is None: |
| 118 | + return None |
| 119 | + node = root |
| 120 | + while node.left: |
| 121 | + if node.left.val < low: |
| 122 | + node.left = node.left.right |
| 123 | + else: |
| 124 | + node = node.left |
| 125 | + node = root |
| 126 | + while node.right: |
| 127 | + if node.right.val > high: |
| 128 | + node.right = node.right.left |
| 129 | + else: |
| 130 | + node = node.right |
| 131 | + return root |
| 132 | +``` |
| 133 | + |
76 | 134 | ### **Java**
|
77 | 135 |
|
78 | 136 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
@@ -111,6 +169,51 @@ class Solution {
|
111 | 169 | }
|
112 | 170 | ```
|
113 | 171 |
|
| 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 | + */ |
| 188 | +class Solution { |
| 189 | + public TreeNode trimBST(TreeNode root, int low, int high) { |
| 190 | + while (root != null && (root.val < low || root.val > high)) { |
| 191 | + root = root.val < low ? root.right : root.left; |
| 192 | + } |
| 193 | + if (root == null) { |
| 194 | + return null; |
| 195 | + } |
| 196 | + TreeNode node = root; |
| 197 | + while (node.left != null) { |
| 198 | + if (node.left.val < low) { |
| 199 | + node.left = node.left.right; |
| 200 | + } else { |
| 201 | + node = node.left; |
| 202 | + } |
| 203 | + } |
| 204 | + node = root; |
| 205 | + while (node.right != null) { |
| 206 | + if (node.right.val > high) { |
| 207 | + node.right = node.right.left; |
| 208 | + } else { |
| 209 | + node = node.right; |
| 210 | + } |
| 211 | + } |
| 212 | + return root; |
| 213 | + } |
| 214 | +} |
| 215 | +``` |
| 216 | + |
114 | 217 | ### **C++**
|
115 | 218 |
|
116 | 219 | ```cpp
|
@@ -138,6 +241,48 @@ public:
|
138 | 241 | };
|
139 | 242 | ```
|
140 | 243 |
|
| 244 | +```cpp |
| 245 | +/** |
| 246 | + * Definition for a binary tree node. |
| 247 | + * struct TreeNode { |
| 248 | + * int val; |
| 249 | + * TreeNode *left; |
| 250 | + * TreeNode *right; |
| 251 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 252 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 253 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 254 | + * }; |
| 255 | + */ |
| 256 | +class Solution { |
| 257 | +public: |
| 258 | + TreeNode* trimBST(TreeNode* root, int low, int high) { |
| 259 | + while (root && (root->val < low || root->val > high)) { |
| 260 | + root = root->val < low ? root->right : root->left; |
| 261 | + } |
| 262 | + if (!root) { |
| 263 | + return root; |
| 264 | + } |
| 265 | + TreeNode* node = root; |
| 266 | + while (node->left) { |
| 267 | + if (node->left->val < low) { |
| 268 | + node->left = node->left->right; |
| 269 | + } else { |
| 270 | + node = node->left; |
| 271 | + } |
| 272 | + } |
| 273 | + node = root; |
| 274 | + while (node->right) { |
| 275 | + if (node->right->val > high) { |
| 276 | + node->right = node->right->left; |
| 277 | + } else { |
| 278 | + node = node->right; |
| 279 | + } |
| 280 | + } |
| 281 | + return root; |
| 282 | + } |
| 283 | +}; |
| 284 | +``` |
| 285 | + |
141 | 286 | ### **Go**
|
142 | 287 |
|
143 | 288 | ```go
|
@@ -165,6 +310,46 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode {
|
165 | 310 | }
|
166 | 311 | ```
|
167 | 312 |
|
| 313 | +```go |
| 314 | +/** |
| 315 | + * Definition for a binary tree node. |
| 316 | + * type TreeNode struct { |
| 317 | + * Val int |
| 318 | + * Left *TreeNode |
| 319 | + * Right *TreeNode |
| 320 | + * } |
| 321 | + */ |
| 322 | +func trimBST(root *TreeNode, low int, high int) *TreeNode { |
| 323 | + for root != nil && (root.Val < low || root.Val > high) { |
| 324 | + if root.Val < low { |
| 325 | + root = root.Right |
| 326 | + } else { |
| 327 | + root = root.Left |
| 328 | + } |
| 329 | + } |
| 330 | + if root == nil { |
| 331 | + return nil |
| 332 | + } |
| 333 | + node := root |
| 334 | + for node.Left != nil { |
| 335 | + if node.Left.Val < low { |
| 336 | + node.Left = node.Left.Right |
| 337 | + } else { |
| 338 | + node = node.Left |
| 339 | + } |
| 340 | + } |
| 341 | + node = root |
| 342 | + for node.Right != nil { |
| 343 | + if node.Right.Val > high { |
| 344 | + node.Right = node.Right.Left |
| 345 | + } else { |
| 346 | + node = node.Right |
| 347 | + } |
| 348 | + } |
| 349 | + return root |
| 350 | +} |
| 351 | +``` |
| 352 | + |
168 | 353 | ### **...**
|
169 | 354 |
|
170 | 355 | ```
|
|
0 commit comments