mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-15 07:20:40 +08:00
Update 0404.左叶子之和.md
勘误python注释
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
@ -242,37 +242,10 @@ 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 # 中
|
||||
```
|
||||
> 递归前序遍历
|
||||
```python3
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def sumOfLeftLeaves(self, root: TreeNode) -> int:
|
||||
# 需要通过中节点来判断其的左节点是否存在;左节点自己的左右节点也是否存在
|
||||
|
||||
if not root: return 0
|
||||
|
||||
# 初始化left_leaf备用
|
||||
left_leaf = 0
|
||||
# 若当前节点的左孩子就是左叶子
|
||||
if root.left and not root.left.left and not root.left.right:
|
||||
left_leaf = root.left.val
|
||||
|
||||
left_left_leaves_sum = self.sumOfLeftLeaves(root.left)
|
||||
right_left_leaves_sum = self.sumOfLeftLeaves(root.right)
|
||||
|
||||
|
||||
return left_leaf + left_left_leaves_sum + right_left_leaves_sum
|
||||
```
|
||||
|
||||
|
||||
> 迭代
|
||||
```python3
|
||||
|
Reference in New Issue
Block a user