EasyTreeDepth-First SearchBinary Tree

1 approach, code in every language

Problem description

You are given the root of a binary tree. The tree is height-balanced when, for every node, the heights of its left and right subtrees differ by no more than one.

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

Example 1

Input: root = [3,9,20,null,null,15,7]

Output: true

Every node's two subtrees differ in height by at most 1.

Example 2

Input: root = [1,2,2,3,3,null,null,4,4]

Output: false

The left subtree is much deeper than the right, so the root fails the balance condition.

Constraints

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

After the pattern

Now solve one you haven't seen.

Scaffold Stands Steady is our own problem on the technique behind Balanced Binary Tree: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free