Merge pull request #111 from Joshua-Lu/patch-29

更新 0669.修剪二叉搜索树 Java版本
This commit is contained in:
Carl Sun
2021-05-14 10:59:31 +08:00
committed by GitHub

View File

@ -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;
if (root == null) {
return null;
}
private static TreeNode trim(TreeNode root,int low, int high) {
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;
}
}