check if a tree is a binary search tree

前端 未结 9 898
遥遥无期
遥遥无期 2021-01-30 09:29

I have written the following code to check if a tree is a Binary search tree. Please help me check the code:

Okay! The code is edited now. This simple solution was sugge

9条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 09:54

    public void inorder()
         {
             min=min(root);
             //System.out.println(min);
             inorder(root);
    
         }
    
         private int min(BSTNode r)
             {
    
                 while (r.left != null)
                 {
                    r=r.left;
                 }
              return r.data;
    
    
         }
    
    
         private void inorder(BSTNode r)
         {
    
             if (r != null)
             {
                 inorder(r.getLeft());
                 System.out.println(r.getData());
                 if(min<=r.getData())
                 {
                     System.out.println(min+"<="+r.getData());
                     min=r.getData();
                 }
                 else
                 System.out.println("nananan");
                 //System.out.print(r.getData() +" ");
                 inorder(r.getRight());
                 return;
             }
         }
    

提交回复
热议问题