842. Count Leaf Nodes

EasyTreesBinary TreeDFSRecursion

Given the root of a binary tree, return the number of leaf nodes (nodes with no children). The tree is given as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Integer — the leaf count.

Examples

Example 1
Input: [1,2,3,4,5,null,6]
Output: 3
Explanation: Leaves are 4, 5, and 6.
Example 2
Input: [1]
Output: 1
Explanation: Root is a leaf.
Example 3
Input: []
Output: 0
Explanation: Empty tree.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →