Given a binary tree, determine if it is
.
Example 1:
Input: root = [3,9,20,null,null,15,7] Output: true
Example 2:
Input: root = [1,2,2,3,3,null,null,4,4] Output: false
Example 3:
Input: root = [] Output: true
Constraints:
[0, 5000]
.-104 <= Node.val <= 104
class Solution {
boolean balanceFactor = true ;
public int height(TreeNode root){
if(root==null){
return 0 ;
}
int lh = height(root.left);
int rh = height(root.right);
if(Math.abs(lh-rh)>1){
balanceFactor = false ;
}
return Math.max(lh , rh ) +1 ;
}
public boolean isBalanced(TreeNode root) {
int h = height(root);
return balanceFactor ;
}
}
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India