求深度,左右子树深度做差值;
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;
}
}