这些学年已经有一段时间了.在医院找到了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)
这是一个很好的实现吗?还是我错过了什么?