Problems
Tree Sum of left leaves
Tree Sum of left leaves
Given a binary tree. Find the sum of all its left leaves.
Definition of a tree:
\begin{lstlisting}[language=Java]
// Java
class TreeNode
{
public:
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
left = NULL;
right = NULL;
};
\end{lstlisting}
\begin{lstlisting}[language=C++]
// C++
class TreeNode
{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
\end{lstlisting}
Implement a function \textbf{sumLeft} that returns the sum of left leaves in the tree. If input tree haven't left leaves, return $0$.
\begin{lstlisting}[language=C++]
// Java
int sumLeft(TreeNode tree)
// C++
int sumLeft(TreeNode *tree)
\end{lstlisting}
\Note
\includegraphics{https://static.e-olymp.com/content/2f/2ff22c19625e6a2a9f40d0dbadf15be3fe9d7be0.gif}
Function \textbf{sumLeft} returns $14 = 5 + 9$.