标签:return leave href for solution ref str div tle
计算给定二叉树的所有左叶子之和。
3
/ 9 20
/ 15 7
在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
/**
* 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) {
int res = 0;
if(root == NULL || (root->left == NULL && root->right == NULL)){
return 0;
}else{
if(root->left != NULL){
if(root->left->left == NULL && root->left->right == NULL){
res += root->left->val;
}else{
res += sumOfLeftLeaves(root->left);
}
}
if(root->right != NULL){
if(root->right->left == NULL && root->right->right == NULL){
}else{
res += sumOfLeftLeaves(root->right);
}
}
return res;
}
}
};
leetcode 404. 左叶子之和(Sum of Left Leaves)
标签:return leave href for solution ref str div tle
原文地址:https://www.cnblogs.com/zhanzq/p/10577814.html