问题描述
注意事项
递归条件必须注意
问题是求所有左叶子之和
代码
/**
* 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!=NULL&&root->left->left==NULL&&root->left->right==NULL) return sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right)+root->left->val;
return sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right);
}
};