A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int SumOfLeftLeaves(TreeNode root)
{
return DFS(root,false);
}
public int DFS(TreeNode root, bool isLeaf)
{
if (root == null)
return 0;
if (root.left == null && root.right == null)
{
return isLeaf ? root.val : 0;
}
return DFS(root.left, true) + DFS(root.right, false);
}
}