题解 | #平衡二叉树#
平衡二叉树
http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
比较每一个节点的左右差值,如果差值小于等于1就递归判断其左右节点是否满足。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(null==root)
return true;
if(1>=Math.abs(getMaxLen(root.left ,0)-getMaxLen(root.right,0)))
return (IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right));
else
return false;
}
public int getMaxLen(TreeNode root,int n) {
if(null==root)
return n;
return Math.max(getMaxLen(root.left,n+1),getMaxLen(root.right,n+1));
}
}