码迷,mamicode.com
首页 > 其他好文 > 详细

404. Sum of Left Leaves

时间:2017-11-01 23:00:51      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:blog   ane   roo   leaves   pull   solution   clear   treenode   null   

 

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.

 1 class Solution {
 2     
 3     public int sumOfLeftLeaves(TreeNode root) {
 4         if(root==null) return 0;
 5         int sum = 0;
 6         if(root.left!=null){
 7             if(root.left.left==null && root.left.right==null) sum +=root.left.val;
 8             else sum+=sumOfLeftLeaves(root.left);
 9         }
10         sum+=sumOfLeftLeaves(root.right);
11         return sum;
12         
13     }
14  
15 }

 

404. Sum of Left Leaves

标签:blog   ane   roo   leaves   pull   solution   clear   treenode   null   

原文地址:http://www.cnblogs.com/zle1992/p/7768759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!