読者です 読者をやめる 読者になる 読者になる

Check if a binary tree is balanced

Binary Tree Tree Height Recursive Cracking The Coding Interview

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.