861. Check if Binary Tree is Perfect

EasyTreesBinary TreeDFSRecursion

Given the root of a binary tree, determine whether it is perfect — all internal nodes have two children and all leaves are at the same depth. 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,6,7]
Output: true
Explanation: All leaves at the same depth, internal nodes have two children.
Example 2
Input: [1,2,3,4,5,6]
Output: false
Explanation: Last level is not full.
Example 3
Input: [1]
Output: true
Explanation: Single node.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →