Skip to content
BASA DSA

Balanced Binary Tree

EasyTrees

Return whether every node has left and right subtree heights differing by at most one.

Example 1

Argument 1 tree: level-order input; omitted children are null.Argument 1 tree: level-order input; omitted children are null. Vertices: node 0: 1, node 1: 2, node 2: 3, node 3: 4. Edges: node 0 (1) — node 1 (2) left child; node 0 (1) — node 2 (3) right child; node 1 (2) — node 3 (4) left child.1234
Argument 1 tree: level-order input; omitted children are null.

Input
root = [1,2,3,4]
Output
true

Explanation

Node 2 has one child and the root's heights differ by one; all nodes are balanced.

Example 2

Argument 1 tree: level-order input; omitted children are null.Argument 1 tree: level-order input; omitted children are null. Vertices: node 0: 1, node 1: 2, node 2: 3. Edges: node 0 (1) — node 1 (2) left child; node 1 (2) — node 2 (3) left child.123
Argument 1 tree: level-order input; omitted children are null.

Input
root = [1,2,null,3]
Output
false

Explanation

The root has left height two and right height zero, violating balance.

Example 3

Argument 1 tree: empty tree (null root).
Input
root = []
Output
true

Explanation

An empty tree has no violating node.

Constraints

  • 0 <= number of nodes <= 5000
  • Trees are breadth-first arrays with null holes

Function contractisBalancedArguments: tree
Loading draft…
SolutionRuns on your device
Loading draft…