Sum of Left Leaves#4003

Sum of Left Leaves

1 second
128 megabytes
Easy (≈1000)

Given an array of integers. Create a Binary Search Tree from these numbers. If the inserted value equals the current node, insert it into the right subtree.

Implement the method SumLeft, which returns the sum of all left leaves in the tree.

prb7463.gif

Write the code according to the following 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); // Insert value val into the Binary Search Tree
  int SumLeft(void); // Return the sum of all left leaves in the tree
};

You can create (use) additional methods if necessary.

Input

The first line contains the number (1 \le n \le 100). The second line contains integers.

Output

Create a Binary Search Tree from the input data. Print the sum of all left leaves in the tree. If the tree does not contain left leaves, print 0.

Examples

Example 1

Loading
Loading