Merge pull request #820 from casnz1601/patch-4

Update 0404.左叶子之和.md
This commit is contained in:
程序员Carl
2021-10-10 08:43:24 +08:00
committed by GitHub

View File

@ -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:
""" """