e-olymp
favorite We need a little bit of your help to keep things running, click on this banner to learn more
Competitions

class Tree

Tree Sum of left leaves

Given a binary tree. Find the sum of all left leaves.

Definition of a tree:

// Java
class TreeNode
{
public:
  int val;
  TreeNode left;
  TreeNode right;
  TreeNode(int x) {
    val = x;
    left = NULL; 
    right = NULL;
};
// C++
class TreeNode
{
public:
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

Implement function sumLeft that returns the sum of left leaves in a tree. If input tree haven't left leaves, return 0.

// Java
int sumLeft(TreeNode tree)
// C++
int sumLeft(TreeNode *tree)

Example

prb10111.gif

Function sumLeft returns 14 = 5 + 9.

Time limit 1 second
Memory limit 128 MiB
Author Mykhailo Medvediev