题解 | #平衡二叉树#
平衡二叉树
http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
public class Solution {
public int F(TreeNode root){
if(root==null) return 0;
int left=0,right=0,re = 1;
if(root.left!=null) left = F(root.left);
if(root.right!=null) right = F(root.right);
return re + (Math.max(left,right));
}
public boolean IsBalanced_Solution(TreeNode root) {
if(root==null) return true;
int left=0,right=0;
if(root.left!=null) left = F(root.left);
if(root.right!=null) right = F(root.right);
if(Math.abs(left-right)<2)
return (IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right));
return false;
}
}