Problems
Sum of Left Leaves
Sum of Left Leaves
Given an array of integers. Create a Binary Search Tree from these numbers. If the inserted value equals to the current node, insert it to the right subtree.
Write method SumLeft that returns the sum of all left leaves in a tree.
Write the code according to the next interface:
class TreeNode
{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Tree
{
public:
TreeNode *head;
Tree() : head(NULL) {};
void Insert(int val); // Вставка числа val в Бинарное Дерево Поиска
int SumLeft(void); // Вернуть сумму всех левых листов в дереве
};
You can create (use) additional methods if needed.
Input data
The first line contains number n (1 ≤ n ≤ 100). The second line contains n integers.
Output data
Create the Binary Search Tree from input data. Print the sum of all left leaves in a tree. If the tree does not contain left leaves, print 0.
Examples
Input example #1
6 14 9 1 14 20 13
Output example #1
1