Balanced Binary Tree

A balanced binary tree is defined as a tree such that either it is an empty tree, or all its subtree are balanced and has a height difference of at most 1.

In that case, given a binary tree, determine if it's balanced.


  • tree: A binary tree.


  • A boolean representing whether the tree given is balanced.


Example 1


Output: true

Explanation: By definition, this is a balanced binary tree.

Example 2


Output: false

Explanation: The subtrees of the node labelled 3 has a height difference of 2, so it is not balanced.

Try it yourself




Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

1  >>> a = [1, 2, 3]
2  >>> a[-1]
3  3

Get premium for instant access to all content and solutions