algorithm - 尝试检查树是否为二叉搜索树

标签 algorithm tree binary-tree binary-search-tree

首先,我根据 Level order(又名 Breadth first)遍历将树插入到数组中。 现在我检查数组

 For i=1 to Len(Array) do:
         IF 2*i smaller than Len(Array) then:
            IF Array[i] smaller than Array[2i] OR Array[i] larger than  Array[2i+1] then:
                 Return false
         Else if 2*I larger than Len(Array) then 
            Return True

但我的问题是算法只有在树是完全二叉树时才有效

最佳答案

作为提示,当且仅当树的中序遍历按排序顺序列出键时,二叉树才是二叉搜索树。尝试从逐层遍历切换到中序遍历并进行适当的修改。

希望这对您有所帮助!

关于algorithm - 尝试检查树是否为二叉搜索树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20547645/

相关文章:

javascript - 在更长的数字中查找序列的算法

algorithm - 红黑树中具有相同字符串的最大整数

java - 使用尾递归的 DoublyLinkedList - InsertItem

Java Huffman树代码 "Decode"方法不起作用

c++ - 是一个二叉树 BST 但只有右 child 才允许重复

algorithm - 多代理寻路 - 无交叉路径

java - 在 Java 语言层次结构中添加自定义树类

c - 知识树中的段错误

c++ - 用于快速搜索的二进制数据结构

algorithm - 二叉树访问: get from one leaf to another leaf