public class Solution {
private boolean balanced = true;
public boolean isBalanced(TreeNode root) {
findDepth(root, 1);
return balanced;
}
private int findDepth(TreeNode node, int level){
if (!balanced  node == null){
return level1;
}
int depthL = findDepth(node.left, level+1);
int depthR = findDepth(node.right, level+1);
int max = Math.max(depthL, depthR);
int min = Math.min(depthL, depthR);
if (maxmin > 1){
balanced = false;
}
return max;
}
}
My Simple DFS Java solution


Along with using Math.abs() , You can also have 1 more modification
as shown ..private int findDepth(TreeNode node, int level) { if (node == null) return level1; int depthL =0 ,depthR=0; if(balanced) { depthL = findDepth(node.left, level+1); depthR = findDepth(node.right, level+1); if (Math.abs(depthL,depthR) > 1) balanced = false; } return depthL>depthR?depthL:depthR; }