1.#### 左葉子之和
計(jì)算給定二叉樹的所有左葉子之和。
示例:
3
/
9 20
/
15 7
在這個(gè)二叉樹中耸弄,有兩個(gè)左葉子郊闯,分別是 9 和 15肌幽,所以返回 24
/**
* 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 {
public:
int sumOfLeftLeaves(TreeNode* root) {
vector<int> left;
get_left_leaf( root , left );
int sum = 0;
for( int i = 0 ; i < left.size() ; i++ )
{
sum += left[i];
}
return sum;
}
void get_left_leaf( TreeNode * root , vector<int>& left)
{
if(root == NULL) return ;
if(root -> left != NULL&& root -> left -> left == NULL&&root->left->right==NULL) left.push_back(root->left->val);
get_left_leaf(root->left ,left);
get_left_leaf(root->right ,left);
}
};