/**
* 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 leftHeight = depth(root.left,0);
int leftRight = depth(root.right,0);
if(Math.abs(leftHeight - leftRight) > 1){
return false;
}
if(isBalanced(root.left) && isBalanced(root.right)){
return true;
}else{
return false;
}
}
public int depth(TreeNode node, int height){
if(node ==null){
return height;
}
int lefth = depth(node.left, height + 1 );
int righth = depth(node.right, height + 1 );
return Math.max( lefth,righth );
}
}