Tree Sum of left leaves#10111

Tree Sum of left leaves

1 Sekunde
128 Megabyte
Leicht

Given a binary tree. Find the sum of all its 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 a function sumLeft that returns the sum of left leaves in the tree. If the input tree has no left leaves, return .

// Java
int sumLeft(TreeNode tree)

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

Notiz

Function sumLeft returns 14 = 5 + 9.