问题描述
剑指offer55.平衡二叉树
题目分析:
先创建一个求取二叉树高度的方法,在判断是否为平衡二叉树的方法中,对每个结点都进行比较
代码实现:
public class phTreeNode {
public int TreenodeHight(TreeNode node){
int count=0;
if (node!=null){
count=Math.max(TreenodeHight(node.left),TreenodeHight(node.right))+1;
}
return count;
}
public boolean isBalanced(TreeNode root){
if(root==null) return true;
else
return Math.abs(TreenodeHight(root.left)-TreenodeHight(root.right))<=1&&isBalanced(root.left)&&isBalanced(root.right);
}
}