题目描述
blablabla
样例
blablabla
算法1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null) return true;
int i=Depth(root.left)-Depth(root.right);
return isBalanced(root.left)&&isBalanced(root.right)&&(i>-2)&&(i<=2);
}
public int Depth(TreeNode root)
{
if(root==null) return 0;
return Math.max(Depth(root.left),Depth(root.right))+1;
}
}