美文网首页
404 sum of left leaves

404 sum of left leaves

作者: larrymusk | 来源:发表于2017-12-05 11:05 被阅读0次
    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.
    
    
    
    int sumOfLeftLeaves(struct TreeNode* root) {
        
            if (!root) return 0;
            if (root->left && !root->left->left && !root->left->right)
                return root->left->val + sumOfLeftLeaves(root->right);
            return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
    }
    

    相关文章

      网友评论

          本文标题:404 sum of left leaves

          本文链接:https://www.haomeiwen.com/subject/vcvqixtx.html