原题链接在这里:https://leetcode.com/problems/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.

题解:

DFS, 若root.left不为null时,检查root.left是否为leaf. 若是res+=root.left.val, 若不是继续DFS.

再从root.right做DFS.

Time Complexity: O(n), n是tree的node数目. Space: O(logn).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int sumOfLeftLeaves(TreeNode root) {
12         if(root == null){
13             return 0;
14         }
15         
16         int res = 0;
17         if(root.left != null){
18             if(root.left.left == null && root.left.right == null){
19                 res += root.left.val;
20             }else{
21                 res += sumOfLeftLeaves(root.left);
22             }
23         }
24         
25         res += sumOfLeftLeaves(root.right);
26         
27         return res;
28     }
29 }

Iteration 做法.

Time Complexity: O(n). Space: O(logn).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int sumOfLeftLeaves(TreeNode root) {
12         if(root == null){
13             return 0;
14         }
15         
16         int res = 0;
17         Stack<TreeNode> stk = new Stack<TreeNode>();
18         stk.push(root);
19         while(!stk.isEmpty()){
20             TreeNode cur = stk.pop();
21             if(cur.left != null){
22                 if(cur.left.left == null && cur.left.right == null){
23                     res += cur.left.val;
24                 }else{
25                     stk.push(cur.left);
26                 }
27             }
28             if(cur.right != null){
29                 stk.push(cur.right);
30             }
31         }
32         return res;
33     }
34 }

 BFS也可以做.

Time Complexity: O(n). Space: O(n).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int sumOfLeftLeaves(TreeNode root) {
12         if(root == null){
13             return 0;
14         }
15         
16         int res = 0;
17         LinkedList<TreeNode> que = new LinkedList<TreeNode>();
18         que.offer(root);
19         while(!que.isEmpty()){
20             TreeNode cur = que.poll();
21             if(cur.left != null){
22                 if(cur.left.left == null && cur.left.right == null){
23                     res += cur.left.val;
24                 }else{
25                     que.offer(cur.left);
26                 }
27             }
28             if(cur.right != null){
29                 que.offer(cur.right);
30             }
31         }
32         return res;
33     }
34 }