Day 84 of 100 Days LeetCode Challenge

LeetCode Challenge #110. Balanced Binary Tree

Given a binary tree, determine if it is 

 

.

 

Example 1:

balance 1

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

balance 2

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

 

Constraints:

  • The number of nodes in the tree is in the range [0, 5000].
  • -104 <= Node.val <= 104
Video Solution
Java Solution
				
					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 ;
    }
}
				
			

Happy Coding with edSlash