404. Sum of Left Leaves
【题目】中文版 英文版
1 /**
2 * Definition for a binary tree node.
3 * struct TreeNode {
4 * int val;
5 * TreeNode *left;
6 * TreeNode *right;
7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8 * };
9 */
10 class Solution
11 {
12 public:
13 int sumOfLeftLeaves(TreeNode* root)
14 {
15 int res = 0;
16 solute(root, res, false);
17 return res;
18 }
19
20 void solute(TreeNode *root, int &sum, bool flag)
21 {
22 if(root == nullptr)
23 return;
24 if(!root->left && !root->right)
25 {
26 if(flag == true)
27 sum += root->val;
28 return;
29 }
30 solute(root->left, sum, true);
31 solute(root->right, sum, false);
32 return;
33 }
34 };