首页 > 其他 > 详细

LeetCode题解之 Sum of Left Leaves

时间:2019-02-26 13:06:28      阅读:120      评论:0      收藏:0      [点我收藏+]

1、题目描述

技术分享图片

2、问题分析

对于每个节点,如果其左子节点是叶子,则加上它的值,如果不是,递归,再对右子节点递归即可。

 

3、代码

 1 int sumOfLeftLeaves(TreeNode* root) {
 2         if (root == NULL)
 3             return 0;
 4         int ans = 0;
 5         if (root->left != NULL) {
 6             if (root->left->left == NULL && root->left->right == NULL) 
 7                 ans += root->left->val;
 8             else 
 9                 ans += sumOfLeftLeaves(root->left);
10         }
11         
12         ans += sumOfLeftLeaves(root->right);
13         
14         return ans;
15         
16     }

 

LeetCode题解之 Sum of Left Leaves

原文:https://www.cnblogs.com/wangxiaoyong/p/10436506.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!