这个问题已经在这里讨论过了,但是我有一个下面的实现(该实现在帖子中从未被讨论):
public boolean isBalanced(BSTNode node) {
if(maxHeight() > (int)(Math.log(size())/Math.log(2)) + 1)
return false;
else
return true;
}
其中maxHeight()返回树的最大高度。基本上,我正在检查maxHeight是否大于log(n),其中n是树中元素的数量。这个解决方案正确吗?
node
,也不需要一个if()
语句,你可以直接返回布尔值。否则,对于自平衡二叉搜索树而言,它看起来是正确的。http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree - Peter Lawrey