404.求二叉树左叶子节点之和 Sum of Left Leaves

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. public class Solution {
  2. private int sum = 0;
  3. public int SumOfLeftLeaves(TreeNode root) {
  4. if (root != null) {
  5. if(root.left != null && (root.left.left == null && root.left.right == null)){
  6. sum += root.left.val;
  7. }
  8. SumOfLeftLeaves(root.left);
  9. SumOfLeftLeaves(root.right);
  10. }
  11. return sum;
  12. }
  13. }





posted @ 2017-01-10 23:04  xiejunzhao  阅读(149)  评论(0编辑  收藏  举报