LeetCode 404. Sum of Left Leaves
题目描述:
Find the sum of all left leaves in a given binary tree.
Example:
3 / \ 9 20 / \ 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
用DFS找出所有左叶子节点再求和即可.
/**
* 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);
}
};
评论