All problems

Problem 403

Count Leaf Nodes

Easy · 10 pts · Tree, Depth-First Search, Binary Tree

Problem

A leaf is a node with no children: neither a left child nor a right child. Count the leaves of a binary tree. A tree with a single node has exactly one leaf, the root itself. A recursive solution counts the leaves of the left and right subtrees and adds them up.

Input

The first line contains N, the number of tokens. The second line contains N tokens describing the tree in level order (breadth-first, left to right), where 'null' marks a missing child, as on LeetCode.

Output

Print the number of leaves.

Constraints

1 <= number of nodes <= 10000; -100 <= value <= 100

Example input

7
3 9 20 null null 15 7

Example output

3

Explanation

In the tree 3 / 9 20 / 15 7, the nodes 9, 15 and 7 have no children, so there are 3 leaves.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Largest Value in Each Row Next: Bottom Left Tree Value