53. Check for balanced binary tree

Given a binary tree root, find if it is height-balanced or not.

A tree is height-balanced if the difference between the heights of left and right subtrees is not more than one for all nodes of the tree. 

Example 1:

Input : [3, 9, 20, null, null, 15, 7]

Output : Yes

Explanation :

Example 2:

Input : [1, 2, null, null, 3]

Output : No

Explanation :

Now Your Turn!

Pick the correct output for the given input

Input : root = [5, 1, 2, 8, 3, null, 5, null, 4]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= Number of Nodes <= 105
  • 1 <= Node.val <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
/**
* Definition for a binary tree node.
* struct TreeNode {
* int data;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int val) : data(val) , left(nullptr) , right(nullptr) {}
* };
**/
 
class Solution{
public:
bool isBalanced(TreeNode *root){
//your code goes here
}
};
Test Case

Input:

Root