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