Embercode
✕
Binary TreesCoding problems

Binary Trees: coding problems

Write a program that reads the input and prints the answer. Run checks it against every test, including hidden ones.

0 of 8 solved

  • A forest ranger maps a park's trail system as a tree. The first trail starts at the ranger station, which is the root. Every junction splits a trail into a left branch and a right branch. A **leaf** is a trail end with no branches after it. The ranger wants to know how many trail ends there are, so she can place one signpost at each.

    Print the number of leaves in the tree.

    **Constraints:** 0 <= N <= 5000. Each value is an integer from -1000 to 1000.

    Input. First line: N, the number of entries in the list (0 for an empty tree). Second line: the N entries, separated by spaces. The first entry is the root. After it come the children, row by row from the top, and each parent's left child before its right child. An entry `null` means that position has no node. The list may stop after the last real node. A node is a leaf when both of its children are `null`.

    Output. One integer: the number of leaves.

    Example 1 input

    7
    1 2 3 4 5 null 6
    

    Output

    3
    

    Example 2 input

    0
    
    

    Output

    0