mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
@ -171,10 +171,10 @@ class Solution {
|
||||
int rightValue = sumOfLeftLeaves(root.right); // 右
|
||||
|
||||
int midValue = 0;
|
||||
if (root.left != null && root.left.left == null && root.left.right == null) { // 中
|
||||
if (root.left != null && root.left.left == null && root.left.right == null) {
|
||||
midValue = root.left.val;
|
||||
}
|
||||
int sum = midValue + leftValue + rightValue;
|
||||
int sum = midValue + leftValue + rightValue; // 中
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
@ -230,8 +230,8 @@ class Solution {
|
||||
|
||||
## Python
|
||||
|
||||
**递归**
|
||||
```python
|
||||
**递归后序遍历**
|
||||
```python3
|
||||
class Solution:
|
||||
def sumOfLeftLeaves(self, root: TreeNode) -> int:
|
||||
if not root:
|
||||
@ -242,13 +242,13 @@ class Solution:
|
||||
|
||||
cur_left_leaf_val = 0
|
||||
if root.left and not root.left.left and not root.left.right:
|
||||
cur_left_leaf_val = root.left.val # 中
|
||||
cur_left_leaf_val = root.left.val
|
||||
|
||||
return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum
|
||||
return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum # 中
|
||||
```
|
||||
|
||||
**迭代**
|
||||
```python
|
||||
```python3
|
||||
class Solution:
|
||||
def sumOfLeftLeaves(self, root: TreeNode) -> int:
|
||||
"""
|
||||
|
Reference in New Issue
Block a user