HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Volumes > Data Structures > problem:


50411 - Sum of the Leaves

Guest
• Review clarifications (1)

Volume problems

• 50705 - Student Clubs
• 50779 - The shortest path in a maze
• 180. 50776 - The number of differen...
• 2007.A. 51071 - Phalanx-2
• 50. 50718 - Elevator
• 50711 - Snail Trails
• 50674 - Collecting Eggs
• 50306 - Beautiful Numbers
• 50411 - Sum of the Leaves
• 50675 - Kruja Boys
• 50421 - Repairing road segments
• 50736 - Top N Donors - 2
• 50775 - Balanced Parenthesis
• 50774 - Hot Potato
• 50464 - From Tirana to Durres
• 50757 - National Elections - 1
• 50681 - Center of a Series

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.
Question by Osman Ay.

Sum of the Leaves

A binary search tree (BST), also called ordered binary tree, is a type of binary tree where the nodes are arranged in order: for each node, all elements in its left subtree are less than the node (<), and all the elements in its right subtree are greater than the node (>). BST has the following properties:

  1. Each node has a unique value.
  2. A total order is defined on these values.
  3. The left subtree of a node contains only values less than the node's value.
  4. The right subtree of a node contains only values greater than the node's value.

The tree in the picture is obtained by inserting the values 4, 2, 1, 3, 2, 4, 6, and 5 in that order, starting from an empty tree. In the tree, the value of the root is 4, and the values of leaves are 1, 3 and 5. Make a program that calculates the sum of the leaf nodes' values of the binary search tree that is constructed from a given list of positive integers.

Input specification
Input has two lines. The first line of the input contains an integer N (1 ≤ N ≤ 10000) that represents the number of the integers in the list. The second line contains a list of N integers between 1 and 1000.

Output specification
Output contains a single integer that is the sum of the values of the leaf nodes.

Sample Input I   
  8
  4 2 1 3 2 4 6 5
Sample Input II   
  10
  13 10 10 20 4 17 2 20 11 17
Sample Output I   
  9
Sample Output II   
  30


Для отправки решений необходимо выполнить вход.

www.contester.ru