题目描述:

Find the sum of all left leaves in a given binary tree.

Example:

1
2
3
4
5
6
7
    3
/ \
9 20
/ \
15 7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

用DFS找出所有左叶子节点再求和即可.

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
int sum;
public:
int sumOfLeftLeaves(TreeNode* root) {
sum = 0;
DFS(root, false);
return sum;
}

void DFS(TreeNode *node, bool left){
if(!node) return;
if(!node->left && !node->right && left){
sum += node->val;
return;
}
DFS(node->left, true);
DFS(node->right, false);
}
};