要检查给定的树是否允许重复项是二叉搜索树

问题描述 投票:0回答:1

我知道如何检查给定的树是否为二叉树。但是问题是如果树包含重复值,该怎么办。

如何检查可能包含重复值的树是否为二叉搜索树重复的值必须在树/子树的右侧。

algorithm data-structures tree binary-tree binary-search-tree
1个回答
0
投票

在二进制搜索树程序中,当添加检查右侧树是否为BST时,然后检查右侧值是否大于或等于BST。例如,

bool BST(BinaryTree root){
 bool returnValue=false;
  if(root!=null){
    if(root.left!=null){
      if (root.left.data < root.data){
       returnValue=BST(root.left);
      }
    }
   if(root.right.data >= root.data){
       returnValue=BST(root.right);
    }
   return returnValue;
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.