マツシタのお勉強

Check if a binary tree is balanced

Problem

Implement a function to check if a binary tree is balanced. For the purposes of this question. balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.

How to Solve

This problem can be solved by calculating the height of the one node with recursive. This idea is defined as int getHeight(Node root). If the height of the one node exceeds one, this function returns -1. With this function, we can compose a new function to check if a binary tree is balanced.