#K6426. Balanced Binary Tree
Balanced Binary Tree
Balanced Binary Tree
You are given a binary tree, represented as a list of values in level order where the string "null" represents a missing node. Your task is to determine whether the 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. Formally, for any node, if its left subtree has height \(h_1\) and its right subtree has height \(h_2\), then \(|h_1 - h_2| \le 1\).
Example 1:
Input: 3,9,20,null,null,15,7 Output: true
Example 2:
Input: 1,2,2,3,3,null,null,4,4 Output: false
inputFormat
The input is a single line string representing the binary tree in level order. The values are separated by commas. Use "null" (without quotes) to denote an empty node. An empty input denotes an empty tree.
Example: 3,9,20,null,null,15,7
outputFormat
Output a single line: true
if the tree is height-balanced or false
otherwise.
3,9,20,null,null,15,7
true