/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
Flag f=new Flag();
int depth=getDepth(root,f);
return f.getFlag();
}
public class Flag{
private boolean flag;
public Flag()
{
this.flag=true;
}
public Flag(boolean flag)
{
this.flag=flag;
}
public void setTrue()
{
this.flag=true;
}
public void setFalse()
{
this.flag=false;
}
public boolean getFlag()
{
return this.flag;
}
}
int getDepth(TreeNode temp,Flag f)
{
if(temp==null)
return 0;
int leftDepth=getDepth(temp.left,f);
int rightDepth=getDepth(temp.right,f);
if(((int)Math.abs(leftDepth-rightDepth))<=1&&f.getFlag()==true&&f.getFlag()==true)
f.setTrue();
else
f.setFalse();
return max(leftDepth,rightDepth)+1;
}
int max(int a,int b)
{
return a>b?a:b;
}
}