相关疑难解决方法(0)

如何确定二叉树是否平衡?

这些学年已经有一段时间了.在医院找到了IT专家的工作.现在试着去做一些实际的编程.我现在正在研究二叉树,我想知道确定树是否高度平衡的最佳方法是什么.

我在考虑这个问题:

public boolean isBalanced(Node root){
    if(root==null){
        return true;  //tree is empty
    }
    else{
        int lh = root.left.height();
        int rh = root.right.height();
        if(lh - rh > 1 || rh - lh > 1){
            return false;
        }
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

这是一个很好的实现吗?还是我错过了什么?

java algorithm binary-tree data-structures

111
推荐指数
9
解决办法
12万
查看次数

标签 统计

algorithm ×1

binary-tree ×1

data-structures ×1

java ×1