Balanced Binary Tree - aloalgo

Balanced Binary Tree

Medium

You are given the root of a binary tree.

Your task is to determine if the given binary tree is height-balanced. A binary tree is considered height-balanced if, for every node in the tree, the depth of its left subtree and the depth of its right subtree differ by no more than one.

Note that:

  • An empty tree (a tree with no nodes) is considered height-balanced.

Return true if the binary tree is height-balanced, and false otherwise.

Example 1

Input
123
Output
True
Explanation:

The tree is balanced as both left and right subtrees of the root have a height of 1.

Example 2

Input
12
Output
True
Explanation:

The tree is balanced as the left subtree is empty (height 0) and the right subtree has a height of 1.

Example 3

Input
123
Output
False
Explanation:

For node 1, the left subtree has a height of 2 (path 1->2->3) and the right subtree has a height of 0. The difference is 2, which is greater than 1.

Loading...
Input
123
Output
True

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!