commit
efce1a3df5
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue