#C14680. Balanced Binary Tree Checker

    ID: 44356 Type: Default 1000ms 256MiB

Balanced Binary Tree Checker

Balanced Binary Tree Checker

You are given a binary tree represented in level order as a series of tokens separated by spaces. The token null represents a missing node. Your task is to determine whether the tree is balanced.

A binary tree is considered balanced if for every node in the tree, the difference in heights of its left and right subtrees is at most 1. In mathematical terms, for every node v,

h(left(v))h(right(v))1|h(left(v)) - h(right(v))| \le 1

Print True if the tree is balanced, and False otherwise.

Input Format: The input is given as a single line containing space-separated tokens representing the tree in level order. For example, the tree

    1
   / \
  2   3
 / \
4   5

is represented as:

1 2 3 4 5

and the tree

    1
   /
  2
 /
3

is represented as:

1 2 null 3

inputFormat

The input consists of one line with space-separated tokens that represent the nodes of a binary tree in level order traversal. A token null indicates that the corresponding child is missing. For example:

  • 1 2 3 4 5 represents a balanced binary tree.
  • 1 2 null 3 represents an unbalanced tree, where node 2 has a left child 3.
  • null represents an empty (balanced) tree.

outputFormat

Output a single line: True if the binary tree is balanced, and False otherwise.

## sample
1 2 3 4 5
True

</p>