题解 | #判断是不是平衡二叉树#
判断是不是平衡二叉树
http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
求深度,左右子树深度做差值;
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null){
return true;
}
return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right)&&Math.abs(deep(root.left)-deep(root.right))<2;
}
public int deep(TreeNode root){
if(root == null){
return 0;
}
return Math.max(deep(root.left),deep(root.right))+1;
}
}