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