mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Update 0404.左叶子之和.md
This commit is contained in:
@ -247,8 +247,7 @@ class Solution {
|
||||
|
||||
|
||||
### Python
|
||||
|
||||
**递归后序遍历**
|
||||
递归
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
@ -257,47 +256,64 @@ class Solution {
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
|
||||
if not root:
|
||||
def sumOfLeftLeaves(self, root):
|
||||
if root is None:
|
||||
return 0
|
||||
if root.left is None and root.right is None:
|
||||
return 0
|
||||
|
||||
# 检查根节点的左子节点是否为叶节点
|
||||
if root.left and not root.left.left and not root.left.right:
|
||||
left_val = root.left.val
|
||||
else:
|
||||
left_val = self.sumOfLeftLeaves(root.left)
|
||||
leftValue = self.sumOfLeftLeaves(root.left) # 左
|
||||
if root.left and not root.left.left and not root.left.right: # 左子树是左叶子的情况
|
||||
leftValue = root.left.val
|
||||
|
||||
# 递归地计算右子树左叶节点的和
|
||||
right_val = self.sumOfLeftLeaves(root.right)
|
||||
|
||||
return left_val + right_val
|
||||
rightValue = self.sumOfLeftLeaves(root.right) # 右
|
||||
|
||||
sum_val = leftValue + rightValue # 中
|
||||
return sum_val
|
||||
```
|
||||
递归精简版
|
||||
|
||||
```python
|
||||
# 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):
|
||||
if root is None:
|
||||
return 0
|
||||
leftValue = 0
|
||||
if root.left is not None and root.left.left is None and root.left.right is None:
|
||||
leftValue = root.left.val
|
||||
return leftValue + self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)
|
||||
```
|
||||
|
||||
**迭代**
|
||||
迭代法
|
||||
```python
|
||||
# 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:
|
||||
"""
|
||||
Idea: Each time check current node's left node.
|
||||
If current node don't have one, skip it.
|
||||
"""
|
||||
stack = []
|
||||
if root:
|
||||
stack.append(root)
|
||||
res = 0
|
||||
|
||||
while stack:
|
||||
# 每次都把当前节点的左节点加进去.
|
||||
cur_node = stack.pop()
|
||||
if cur_node.left and not cur_node.left.left and not cur_node.left.right:
|
||||
res += cur_node.left.val
|
||||
|
||||
if cur_node.left:
|
||||
stack.append(cur_node.left)
|
||||
if cur_node.right:
|
||||
stack.append(cur_node.right)
|
||||
|
||||
return res
|
||||
def sumOfLeftLeaves(self, root):
|
||||
if root is None:
|
||||
return 0
|
||||
st = [root]
|
||||
result = 0
|
||||
while st:
|
||||
node = st.pop()
|
||||
if node.left and node.left.left is None and node.left.right is None:
|
||||
result += node.left.val
|
||||
if node.right:
|
||||
st.append(node.right)
|
||||
if node.left:
|
||||
st.append(node.left)
|
||||
return result
|
||||
|
||||
```
|
||||
|
||||
### Go
|
||||
|
Reference in New Issue
Block a user