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