Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created August 13, 2018 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/75684034bc081c7e9ff97a9927c0bb24 to your computer and use it in GitHub Desktop.
Save jianminchen/75684034bc081c7e9ff97a9927c0bb24 to your computer and use it in GitHub Desktop.
Leetcode 404 - sum of left leaves
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Leetcode_404_sum_of_left_leaves
{
/// <summary>
/// Leetcode 404 - sum of left leaves
/// </summary>
class Program
{
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
static void Main(string[] args)
{
}
/// <summary>
/// test case:
/// empty node, tree with one left node.
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public int SumOfLeftLeaves(TreeNode root)
{
if (root == null)
return 0;
var leftLeaves = 0;
if (root.left != null)
{
if(root.left.left == null && root.left.right == null)
leftLeaves += root.left.val;
}
return leftLeaves + SumOfLeftLeaves(root.left) + SumOfLeftLeaves(root.right);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment