Halo

A magic place for coding

0%

LeetCode解题报告(115)-- 404. Sum of Left Leaves

Problem

Find the sum of all left leaves in a given binary tree.

Example:

1
2
3
4
5
6
7
    3
/ \
9 20
/ \
15 7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

Analysis

  二叉树关于叶子节点的问题,还是优先考虑递归的做法。这道题目的难点是如何判断出左叶子节点。总体的思路有两个:

  1. 在递归的时候带上信息,由父节点告诉子节点是否为左节点,然后子节点判断自己是否为叶子节点,结合递归中的信息来判断自己是否为左叶子节点;
  2. 直接在父节点一层判断。

Solution

  上述的两种思路都不复杂,第一种就需要多带一个参数,第二种就可以直接判断,比较简单。


Code

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

Summary

  这道题的分析到这里,谢谢!

Welcome to my other publishing channels