847. Check if Binary Tree is Balanced

EasyTreesBinary TreeDFSRecursion

Given the root of a binary tree, determine whether it is height-balanced — for every node, the heights of its two subtrees differ by at most one. Return true or false. The tree is given as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Boolean — true or false.

Examples

Example 1
Input: [1,2,3,4,5,null,6]
Output: true
Explanation: All subtree heights differ by at most 1.
Example 2
Input: [1,2,null,3,null,4]
Output: false
Explanation: A left-skewed chain is unbalanced.
Example 3
Input: []
Output: true
Explanation: Empty tree is balanced.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →