首先,我根据 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/