#C10194. Balanced Binary Tree
Balanced Binary Tree
Balanced Binary Tree
Given the root of a binary tree, determine whether the tree is height-balanced. A binary tree is said to be height-balanced if for every node in the tree, the absolute difference between the heights of its left subtree and right subtree is no more than 1.
Formally, for every node, let \(L\) and \(R\) be the heights of the left and right subtrees respectively, then the condition to satisfy is:
\( |L - R| \le 1 \)
The tree is given in level-order format where missing nodes are specified by the keyword null
. Your task is to construct the tree from the given input and determine whether it is balanced.
inputFormat
The input consists of a single line containing the level-order traversal of the binary tree nodes separated by spaces. Each value is either an integer or the string null
(representing a missing node). For example, the input "3 9 20 null null 15 7" represents the binary tree:
3 / \ 9 20 / \ 15 7
outputFormat
Print a single line with True
if the binary tree is height-balanced, otherwise print False
.
3 9 20 null null 15 7
True