leetcode_Sum of Left Leaves

来源:互联网 发布:java运维面试题 编辑:程序博客网 时间:2024/06/07 13:20

Find the sum of all left leaves in a given binary tree.
3
/ \
9 20
/ \
15 7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

class Solution {public:    int sumOfLeftLeaves(TreeNode* root) {        int result = 0;        if(root)        {            if(root->left && root->left->left == NULL && root->left->right == NULL)            {                result += root->left->val;            }            result +=  sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right);        }        return result;    }};
0 0
原创粉丝点击