//Best solution: Calculate weight then check the Math.abs(l-r), if -1 set as -1 forever.
/**
* 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) {
if(root == null) return true;
if (getDepth(root)==-1) return false;
return true;
}
public int getDepth(TreeNode root){
if(root == null) return 0;
int l = getDepth(root.left);
int r = getDepth(root.right);
if(l == -1 || r == -1) return -1;
if(Math.abs(l-r)>1) return -1;
return Math.max(l,r)+1;
}
}
//Use BFS to check balanced:
/**
* 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) {
if(root == null) return true;
LinkedList