leetcode 404. Sum of Left Leaves

Orignal question can be found here.

My solution is as follows:

int sumOfLeftLeaves(struct TreeNode* root)
{
  int result = 0;
  int *result_p = &result;
  sumCore(root, 0, result_p);
  return result;
}

void sumCore(struct TreeNode* root, int flag, int *result_p)
{
  if(root == NULL)
    return;
  if(flag==1 && root->left==NULL && root->right==NULL)
    {
      *result_p += root->val;
      return;
    }
  if(root->left != NULL)
    sumCore(root->left, 1, result_p);
  if(root->right != NULL)
    sumCore(root->right, 0, result_p);
}

Leave a Reply

Your email address will not be published. Required fields are marked *