Balanced Binary Tree Problem

Checking for Balanced Binary Tree or Not

You are given a binary tree, and your goal is to determine whether it is height-balanced or not. Return true if the tree is height-balanced and false otherwise.

Binary tree is said to be height-balanced if the difference in height between the left and right subtrees of every node in the tree is no more than 1. This means that for each node, the tree does not lean too heavily to one side. 

To check this, you must calculate the height of the left and right subtrees for each node and ensure the difference is 0 or 1 for all nodes throughout the tree. If any node fails this condition, the tree is not height-balanced.

Checking Balanced Binary Tree

Example 1

Example of Balanced Binary Tree

Example 2

Example 2 of Balanced Binary Tree

Constraints:

  • The number of nodes in the tree is in the range [0, 1000].
  • -1000 <= Node.val <= 1000

Maximum Depth of Binary Tree

Recommendation for Time and Space Complexity – You should aim for a solution with O(n) time and O(n) space, where n is the number of nodes in the tree.

Hints for solving problems

Hint 1 :

A brute force solution would involve traversing every node and checking whether the tree rooted at each node is balanced by computing the heights of its left and right subtrees. This approach would result in an O(n^2) solution. Can you think of a more efficient way? Perhaps you could avoid repeatedly computing the heights for every node by determining balance and height in a single traversal.

Hint 2 :

We can use the Depth First Search (DFS) algorithm to compute the heights at each node. While calculating the heights of the left and right subtrees, we also check if the tree rooted at the current node is balanced. If leftHeight – rightHeight > 1, we update a global variable, such as isBalanced = False. After traversing all the nodes, the value of isBalanced indicates whether the entire tree is balanced or not.

There are mainly 3 approach to solve this problem-

  1. Brute Force Method
  2. Depth First Search Method
  3. Depth First Search(Stack) Method

1. Brute Force Method

In this approach, we find the height of the left and right subtrees for every node and check if the difference is more than 1. If it is, the tree is not balanced. While easy to understand, this method is slow because it repeatedly calculates the height, making it less efficient.

  • Time complexity: O(n^2)
  • Space complexity: O(n)

Code

2. Depth First Search Method

This method uses recursion to calculate the height of each subtree while checking for balance at the same time. If any subtree is unbalanced, we stop further checks. It’s more efficient since we calculate height and balance in one pass.

  • Time complexity: O(n)
  • Space complexity: O(h)
    • Best Case (balanced tree): O(log(n))
    • Worst Case (degenerate tree): O(n)

where n is the number of nodes in the tree and h is the height of the tree.

Code

3. Depth First Search(Stack) Method

Instead of recursion, this approach uses a stack to manually traverse the tree like DFS. It keeps track of subtree heights and balance as we go. This avoids recursion but can be a bit more complex to implement.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

More Articles