Dashboard Temp Share Shortlinks Frames API

HTMLify

LeetCode - Sum of Left Leaves - C
Views: 4 | Author: abh
 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
// Definition for a binary tree node.
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

// @leet start
int sumOfLeftLeaves(struct TreeNode* root) {
    if (!root) {
        return 0;
    }
    struct TreeNode *left = root->left;
    struct TreeNode *right = root->right;
    int sum = 0;
    if (left) {
        sum += sumOfLeftLeaves(left);
    }
    if (right) {
        sum += sumOfLeftLeaves(right);
    }
    if (left && !left->left && !left->right) {
        sum += left->val;
    }
    return sum;
}
// @leet end