LeetCode 1302. Deepest Leaves Sum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


void deepestLeavesSumHelper(struct TreeNode* node, int level, int* sum, int* deepestLevel){
    bool isLeave=true;
    if(node->left){
        deepestLeavesSumHelper(node->left,level+1,sum,deepestLevel);
        isLeave=false;
    }
    if(node->right){
        deepestLeavesSumHelper(node->right,level+1,sum,deepestLevel);
        isLeave=false;
    }
    if(isLeave){
        if(level==*deepestLevel){
            *sum+=node->val;
        }
        else if(level>*deepestLevel){
            *deepestLevel=level;
            *sum=node->val;
        }
    }
}

int deepestLeavesSum(struct TreeNode* root){
    int sum=0,deepestLevel=0;
    deepestLeavesSumHelper(root,0,&sum,&deepestLevel);
    return sum;
}



2 Replies to “LeetCode 1302. Deepest Leaves Sum”

  1. The next occasion Someone said a weblog, I am hoping that this doesnt disappoint me as much as this. Come on, man, I know it was my choice to read, but I just thought youd have something intriguing to talk about. All I hear is a couple of whining about something you could fix when you werent too busy trying to find attention.

Leave a Reply

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