Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Find whether if a Given Binary Tree is Balanced?

Objec­tive: Given a binary tree, Find whether if a Given Binary Tree is Balanced?

What is bal­anced Tree: A bal­anced tree is a tree in which dif­fer­ence between heights of sub-trees of any node in the tree is not greater than one.

Input: A Binary Tree

Out­put: True and false based on whether tree is bal­anced or not.


Balanced Tree Example

Bal­anced Tree Example

Approach :

Naive Approach:
for each node of the tree, get the height of left sub­tree and right sub­tree and check the dif­fer­ence , if it is greater than 1, return false.

Bet­ter Solution:

You may also like...

  • Adir

    why do you need to chek if the right or left height is –1? it’s pos­si­ble that the height would be –1?

    • tuto­ri­al­hori­zon

      Sorry for the con­fu­sion but here Its just a way to rep­re­sent in this prob­lem when tree is not bal­anced, return –1. you can use some other value as well

  • Syed Faisal

    return true;

    I think the con­di­tion must be updated to result >= 0 as a null tree can­not be qual­i­fied as unbalanced