#C6091. Balanced Binary Tree Checker from Level Order
Balanced Binary Tree Checker from Level Order
Balanced Binary Tree Checker from Level Order
Given a binary tree represented in level order as a string, check whether the tree is balanced. A binary tree is defined as balanced if, for every node, the heights of the left and right subtrees differ by at most one.
The level order string consists of node values separated by spaces. A missing child is denoted by the symbol "#". For example, the string "1 2 3 4 5 # #" represents the following tree:
1 / \ 2 3 / \ 4 5
This tree is balanced because at every node, the difference in height between the left and right subtree does not exceed 1.
Your task is to read the input as a single line from standard input (stdin), process the level order representation of the tree, and print "True" if the tree is balanced or "False" otherwise. All formulas are written in LaTeX format. For example, the balance condition can be written as: \( |h_{left} - h_{right}| \le 1 \).
inputFormat
A single line containing the level order traversal of a binary tree. Node values are separated by spaces and missing nodes are represented by a '#' symbol.
outputFormat
A single line: 'True' if the binary tree is balanced, 'False' otherwise.## sample
1 2 3 4 5 # #
True