mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
@ -242,25 +242,22 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
```java
|
|
||||||
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
public TreeNode trimBST(TreeNode root, int low, int high) {
|
public TreeNode trimBST(TreeNode root, int low, int high) {
|
||||||
root = trim(root,low,high);
|
if (root == null) {
|
||||||
return root;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TreeNode trim(TreeNode root,int low, int high) {
|
|
||||||
if (root == null ) return null;
|
|
||||||
if (root.val < low) {
|
if (root.val < low) {
|
||||||
return trim(root.right,low,high);
|
return trimBST(root.right, low, high);
|
||||||
}
|
}
|
||||||
if (root.val > high) {
|
if (root.val > high) {
|
||||||
return trim(root.left,low,high);
|
return trimBST(root.left, low, high);
|
||||||
}
|
}
|
||||||
|
// root在[low,high]范围内
|
||||||
root.left = trim(root.left,low,high);
|
root.left = trimBST(root.left, low, high);
|
||||||
root.right = trim(root.right,low,high);
|
root.right = trimBST(root.right, low, high);
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user