867. Check if Leaf Nodes are at Same Level

EasyTreesBinary TreeDFSBFS

Given the root of a binary tree, determine whether all leaf nodes lie at the same depth. Return true or false. An empty tree is considered true. 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]
Output: true
Explanation: Both leaves are at depth 1.
Example 2
Input: [1,2,3,4,null,null,5,6]
Output: false
Explanation: Leaves at different depths.
Example 3
Input: [1]
Output: true
Explanation: Single leaf.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →