Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用递归修剪二叉搜索树_Java_Data Structures_Binary Search Tree - Fatal编程技术网

Java 使用递归修剪二叉搜索树

Java 使用递归修剪二叉搜索树,java,data-structures,binary-search-tree,Java,Data Structures,Binary Search Tree,给定一个二叉搜索树以及最低和最高边界L和R,修剪树,使其所有元素位于[L,R](R>=L)中。您可能需要更改树的根,因此结果应返回修剪过的二叉搜索树的新根 我是个新手,刚开始学习递归。。我写的代码如下。它可以处理一些测试用例,并为其余的测试用例提供空指针异常。 我知道问题的解决方案(也写在下面),但我想修复我的代码,而不是以编写解决方案的方式 这是我的尝试 /** * Definition for a binary tree node. * public class TreeNode

给定一个二叉搜索树以及最低和最高边界L和R,修剪树,使其所有元素位于[L,R](R>=L)中。您可能需要更改树的根,因此结果应返回修剪过的二叉搜索树的新根

我是个新手,刚开始学习递归。。我写的代码如下。它可以处理一些测试用例,并为其余的测试用例提供空指针异常。 我知道问题的解决方案(也写在下面),但我想修复我的代码,而不是以编写解决方案的方式

这是我的尝试

    /**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
public TreeNode trimBST(TreeNode root, int L, int R) {
    if(root==null)
    {
        return root;
    }
    if(root.val<L)
    {
        root=root.right;
        if(root==null)
        {
            return root;
        }
    }
     if(root.val>R)
    {
        root=root.left;
          if(root==null)
        {
            return root;
        }
    }
     if(root.left!=null)
    {
        if(root.left.val<L)
        {
            root.left=root.left.right;
        }

    }
     if(root.right!=null)
    {
        if(root.right.val>R)
        {
            root.right=root.right.left;
        }

    }
    trimBST(root.left,L,R);
    trimBST(root.right,L,R);
    return root;

}
}
这是解决办法

class Solution {
    public TreeNode trimBST(TreeNode root, int L, int R) {
        if (root == null) return root;
        if (root.val > R) return trimBST(root.left, L, R);
        if (root.val < L) return trimBST(root.right, L, R);

        root.left = trimBST(root.left, L, R);
        root.right = trimBST(root.right, L, R);
        return root;
    }
}
类解决方案{
公共树节点trimBST(树节点根,内部L,内部R){
if(root==null)返回root;
如果(root.val>R)返回trimBST(root.left,L,R);
如果(root.val
我知道我在递归代码的某个地方搞砸了,把一个值设为null,然后再次使用它,我觉得我非常接近解决方案。 我自己无法弄清楚这一点。
请帮帮我。

在这种情况下,它不适用于您的原因是,最后您需要递归地获取新的
根目录。也就是说,
trimBST(root.left,L,R)
将递归地沿着树向下移动,但最终将返回为空。因此,您需要将其分配到树的左侧或右侧

root.left = trimBST(root.left, L, R);
root.right = trimBST(root.right, L, R);

但是在那之后,您还将遇到另一个问题,与
root=root.right
root=root.left。作为一个例子,您也必须在这里使用递归。

在这种情况下,它不适用于您的原因是,最后您需要递归地获取新的
根。也就是说,
trimBST(root.left,L,R)
将递归地沿着树向下移动,但最终将返回为空。因此,您需要将其分配到树的左侧或右侧

root.left = trimBST(root.left, L, R);
root.right = trimBST(root.right, L, R);

但是在那之后,您还将遇到另一个问题,与
root=root.right
root=root.left。作为一个工具,您还必须在这里使用递归。

仔细检查您发布的代码。。。您至少在这里丢失了一些内容
返回trimBST(root.)
root之后有什么内容。
?哪里是
我已经更正了代码和测试用例。。很抱歉现在请看,在结尾处,您需要将
root.left
root.right
分配给一个新值。所以你需要有
root.left=trimBST(root.left,L,R)和树右侧的相同仔细检查您发布的代码。。。您至少在这里丢失了一些内容
返回trimBST(root.)
root之后有什么内容。
?哪里是
我已经更正了代码和测试用例。。很抱歉现在请看,在结尾处,您需要将
root.left
root.right
分配给一个新值。所以你需要有
root.left=trimBST(root.left,L,R)和树的右侧相同