Java 为什么检查二叉树有效性的解决方案不起作用?

Java 为什么检查二叉树有效性的解决方案不起作用?,java,recursion,binary-search-tree,Java,Recursion,Binary Search Tree,我正在解决一个问题,检查二叉树是否是有效的二叉树。这意味着某个节点的左节点的值较小,而右节点的值大于该节点的值。程序使用递归方法执行检查并返回布尔值以确定树的有效性。我提供的代码如下: class Node { Node left; Node right; int data; public Node(int data_ ){ this.data = data_; } } public class myTest{ p

我正在解决一个问题,检查二叉树是否是有效的二叉树。这意味着某个节点的左节点的值较小,而右节点的值大于该节点的值。程序使用递归方法执行检查并返回布尔值以确定树的有效性。我提供的代码如下:

class Node {

      Node left;
      Node right; 
       int data;

     public Node(int data_ ){

       this.data = data_;
   }
}

public class myTest{


private static List<Integer> lis = new ArrayList<Integer>();

public static List<Integer> inOrder(Node root){

    if (root != null){

        inOrder(root.left);

        lis.add(root.data);
        System.out.println( root.data );
        inOrder(root.right);
    }

    return lis;
}

public static boolean isBST( Node root, int min, int max  ){

    if (root ==  null )
        return true;

    return ( root.data >= min) 
        && ( root.data <= max ) 
        && isBST(root.left, min, root.data) 
        && isBST( root.right, root.data, max );
}

public static void main(String[] args){  

// form the bst
 Node root = new Node(5);
   root.left = new Node(3);
   root.right = new Node(7); 

   root.left.left = new Node(1);
   root.left.right = new Node(2);

   root.right.left = new Node(6);
   root.right.right = new Node(9);

   // get the max and min value from the tree
   int max = 0;
   int min  = 0;

   if (root != null ){

      List<Integer> li  = inOrder(root);
      min =  li.get(0);
      max = li.get( li.size() -1 );
   }


   // determine the validity 
   boolean bol_ = isBST(root, min, max );

   if ( bol_)
     System.out.println("valid BST ");

    else 
        System.out.println("Not valid BST ");
  }

  }
public static boolean isBST( Node root, int min, int max  ){

    if (root ==  null )
        return true;

    return ( root.data > min) 
        && ( root.data < max ) 
        && isBST(root.left, min, root.data) 
        && isBST( root.right, root.data, max );
}
类节点{
左淋巴结;
节点权;
int数据;
公共节点(整型数据){
this.data=数据u;
}
}
公共类myTest{
private static List lis=new ArrayList();
公共静态列表顺序(节点根){
if(root!=null){
顺序(根。左);
lis.add(root.data);
System.out.println(root.data);
顺序(root.right);
}
返回lis;
}
公共静态布尔isBST(节点根、最小整数、最大整数){
if(root==null)
返回true;
返回(root.data>=min)

&&(root.data正如@Thomas指出的,您的树不是有效的BST

然而,我想给你一些建议。当你试图递归地评估一棵树是否是二叉树时,有很多边缘情况。一个更简单的解决方案是按顺序遍历树,将元素存储在ArrayList中,然后检查列表是否排序。这个实现更容易/更简单,还可以运行In时间复杂度相同,但空间复杂度更大:两者都是O(n)。

因此:

root.left=新节点(3);
root.left.right=新节点(2);

->在右边,必须有比根更高的值-如下所示

root.left=新节点(2);

root.left.right=新节点(3);

我对方法做了一些更改,现在它如下所示:

class Node {

      Node left;
      Node right; 
       int data;

     public Node(int data_ ){

       this.data = data_;
   }
}

public class myTest{


private static List<Integer> lis = new ArrayList<Integer>();

public static List<Integer> inOrder(Node root){

    if (root != null){

        inOrder(root.left);

        lis.add(root.data);
        System.out.println( root.data );
        inOrder(root.right);
    }

    return lis;
}

public static boolean isBST( Node root, int min, int max  ){

    if (root ==  null )
        return true;

    return ( root.data >= min) 
        && ( root.data <= max ) 
        && isBST(root.left, min, root.data) 
        && isBST( root.right, root.data, max );
}

public static void main(String[] args){  

// form the bst
 Node root = new Node(5);
   root.left = new Node(3);
   root.right = new Node(7); 

   root.left.left = new Node(1);
   root.left.right = new Node(2);

   root.right.left = new Node(6);
   root.right.right = new Node(9);

   // get the max and min value from the tree
   int max = 0;
   int min  = 0;

   if (root != null ){

      List<Integer> li  = inOrder(root);
      min =  li.get(0);
      max = li.get( li.size() -1 );
   }


   // determine the validity 
   boolean bol_ = isBST(root, min, max );

   if ( bol_)
     System.out.println("valid BST ");

    else 
        System.out.println("Not valid BST ");
  }

  }
public static boolean isBST( Node root, int min, int max  ){

    if (root ==  null )
        return true;

    return ( root.data > min) 
        && ( root.data < max ) 
        && isBST(root.left, min, root.data) 
        && isBST( root.right, root.data, max );
}

你的树无效,即
root.left.right.data
小于
root.left.data
(2<3)。哎呀,我现在明白了,谢谢你的提醒。你看到了任何边缘情况或改进代码的建议吗?嗯,我没有彻底阅读代码,但我会尝试分享一些观察结果:1)我不会使用像
inoorder()
这样的静态方法与像
lis
这样的静态变量结合使用,我要么将列表作为参数传递,要么将两个实例都变为变量。2)有人可能会说
node.left.data==node.data
可能不代表有效的树,因为这可能表示节点及其左侧(或右)孩子是一样的。至少你应该检查一下。我同意你所说的传递列表参数。顺便说一句,我在哪里做这个节点。left.data==node.data?你没有显式地这样做,但是你在检查
root.data>=min
等。其中
min
可能隐式地是父节点的值。我宁愿检查一下对于较低值和较大值,在初次调用时,我只需传递
min-1
max+1
。我认为您指出了一个很好的解决方案。正如您在我的代码中看到的,我还制作了一个inOrderTraverse值的数组列表,我可以使用该方法。感谢您的回答。您是对的,我不接受作为t的原因他的观点早些时候由@Thomas指出