Leetcode 404. Sum of Left Leaves
Posted Z-Pilgrim
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 404. Sum of Left Leaves相关的知识,希望对你有一定的参考价值。
https://leetcode.com/problems/sum-of-left-leaves/description/
/**
* 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:
bool isLeave(TreeNode* root)
if (!root) return false;
if (!root->left && !root->right) return true;
return false;
int sumOfLeftLeaves(TreeNode* root)
if (isLeave(root)) return 0;
return sumOfLeftLeaves_(root);
int sumOfLeftLeaves_(TreeNode* root)
if (!root) return 0;
if (isLeave(root)) return root->val;
return sumOfLeftLeaves_(root->left) + ( isLeave(root->right) ? 0 : sumOfLeftLeaves_(root->right) ) ;
;
以上是关于Leetcode 404. Sum of Left Leaves的主要内容,如果未能解决你的问题,请参考以下文章
#Leetcode# 404. Sum of Left Leaves
leetcode-404. Sum of Left Leaves
[leetcode-404-Sum of Left Leaves]
LeetCode 404. Sum of Left Leaves