Problems > Count the Leaves

Count the Leaves

Difficulty: 1100 4000 ms 128 MB

trees

The tree is rooted at node 1. A leaf is a non-root node with no children. Count the leaves (a single-node tree has 1).

Input: the first line has N; each of the next N−1 lines has an edge u v.

Output: the number of leaves.

Constraints:

  • 1 ≤ N ≤ 105

Sample Tests

Input 1
5
1 2
1 3
3 4
3 5
Output 1
3
Input 2
2
1 2
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.