#C11387. Balanced Binary Tree
Balanced Binary Tree
Balanced Binary Tree
You are given the level-order traversal of a binary tree. Your task is to determine whether the binary tree is height-balanced. A binary tree is height-balanced if for every node, the absolute difference between the heights of its left and right subtrees is at most 1. The height of an empty subtree is taken as 0.
The level-order traversal is given as space-separated tokens. A token "null" indicates the absence of a node.
For example, the tree given by the sequence:
1 2 3 4 5
represents the binary tree:
1 / \ 2 3 / \ 4 5
This tree is balanced. However, if the tree were modified such that one subtree is deeper than the other by more than 1 level, it would be considered unbalanced.
inputFormat
The input is provided as a single line containing space-separated tokens representing the level-order traversal of a binary tree. Each token is either an integer (node value) or the string "null" indicating an absent node.
Examples:
1 2 3 4 5
1 2 3 4 5 null null null null 6
null
outputFormat
Print True
if the binary tree is balanced and False
otherwise. The output should be exactly one of these two strings.
1 2 3 4 5
True