[leetcode]404. Sum of Left Leaves
Analysis
everyday is the first day of your rest life—— [间歇性迷茫~]
Find the sum of all left leaves in a given binary tree.
计算二叉树的所有左叶子节点的和,递归遍历。
Implement
/**
* 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) {
if(root == NULL)
return 0;
if(root->left && root->left->left == NULL && root->left->right == NULL)
return root->left->val + sumOfLeftLeaves(root->right);
else
return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
}
};