Is Binary Tree Balanced

Problem Statement

deeksha sharma
Algorithm Problems
1 min readNov 26, 2015

--

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Approach

  • Since, the difference in the depth of two subtrees rooted at each node should not be more than 1, we have to check the following conditions:
  • If the root node is null, then the tree is balanced
  • If the two subtrees rooted under the root are of different height then its not a height balanced binary tree. Return false
  • If two subtrees rooted under the root are of same height then check recursively if each node (left child and right child) are balanced or not. Return true if they are balanced and false otherwise

Code

--

--

deeksha sharma
Algorithm Problems

Work for https://bonsaiilabs.com/ life long learner ,investing time heavily in personal finance, education, tech and design skills. Twitter: @deekshasharma25