Java 正在检查二进制搜索树是否有效[HackerRank]

Java 正在检查二进制搜索树是否有效[HackerRank],java,recursion,binary-tree,binary-search-tree,Java,Recursion,Binary Tree,Binary Search Tree,我正在尝试检查BST是否有效。下面是我的代码。来自HackerRank的输入1234567我的代码总是返回False,即使BST有效 /* Hidden stub code will pass a root argument to the function below. Complete the function to solve the challenge. Hint: you may want to write one or more helper functions. The Nod

我正在尝试检查BST是否有效。下面是我的代码。来自HackerRank的输入1234567我的代码总是返回False,即使BST有效

/* Hidden stub code will pass a root argument to the function below. Complete the function to solve the challenge. Hint: you may want to write one or more helper functions.  

The Node class is defined as follows:
    class Node {
    int data;
    Node left;
    Node right;
     }
*/
    boolean checkBST(Node root) {
        return isValidBSTHelper(root, root.data);

    }

    private boolean isValidBSTHelper(Node root, int limit) {

        if (root == null) return true;
        if (root.left != null) {
           if (root.left.data > root.data || root.left.data > limit) return false;
        }
        if (root.right != null) {
           if (root.right.data < root.data || root.right.data < limit) return false;
        }
        return (isValidBSTHelper(root.left, root.data) &&  isValidBSTHelper(root.right, root.data));
    }
/*隐藏存根代码将向下面的函数传递根参数。完成解决挑战的功能。提示:您可能需要编写一个或多个助手函数。
节点类的定义如下:
类节点{
int数据;
左淋巴结;
节点权;
}
*/
布尔checkBST(节点根){
返回isvalidSelper(root,root.data);
}
专用布尔值IsValidSelper(节点根,整数限制){
if(root==null)返回true;
if(root.left!=null){
if(root.left.data>root.data | | root.left.data>limit)返回false;
}
if(root.right!=null){
if(root.right.data
问题在于第二个if语句。下面解决了这个问题

/* The Node class is defined as follows:

        class Node {
        int data;
        Node left;
        Node right;
         }
    */
        boolean checkBST(Node root) {
            return isValidBSTHelper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);

        }

        private boolean isValidBSTHelper(Node root, int min, int max) {

            if (root == null) return true;
            if (root.data > max || root.data < min) return false;
            return (isValidBSTHelper(root.left, min, root.data) &&  isValidBSTHelper(root.right, root.data, max));
/*节点类定义如下:
类节点{
int数据;
左淋巴结;
节点权;
}
*/
布尔checkBST(节点根){
返回isvalidSelper(根,Integer.MIN\u值,Integer.MAX\u值);
}
专用布尔值isValidBSTHelper(节点根、int-min、int-max){
if(root==null)返回true;
如果(root.data>max | | root.data

}HackerRank in这是一个二进制搜索树吗?

需要->检查条件为:

if (root.data >= max || root.data <= min) return false;

if(root.data>=max | | root.data您正在使用的二进制文件的定义是什么?有几种类型,如果您指的是一个最大值为2个节点的树,那么它可以很容易地解决。您应该提供一个完整的可编译代码示例。我们甚至无法判断树中的值是否真的是图片中的值。您的方法应该d是静态的。您应该使用调试器来查找返回false的行。您还应该使用无效树(如将19替换为51)来查找错误。这是完整的代码。我正在使用HackerRank。输入由HackerRank提供。我还更新了问题。“约束:0