leetcode-404. Sum of Left Leaves 求左叶子节点的和,递归
来源:互联网 发布:人工智能的威胁 编辑:程序博客网 时间:2024/06/06 00:22
题目:
Find the sum of all left leaves in a given binary tree.
Example:
3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
题意:
求左叶子节点的和
代码:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def sumOfLeftLeaves(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None :
return 0
tempnode = root.left;
if tempnode != None and tempnode.left == None and tempnode.right == None :
return tempnode.val + self.sumOfLeftLeaves(root.right)
else:
return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)
笔记:
1、关键是将左叶子节点定义出来
2、python中逻辑运算符 ‘且’ 的表示方法是 and , '或' 的表示方法是 or
- leetcode-404. Sum of Left Leaves 求左叶子节点的和,递归
- [LeetCode] Sum of Left Leaves 左叶子节点的和
- 404. Sum of Left Leaves求树的所有左叶子节点的和
- Leetcode 404. Sum of Left Leaves 左叶子节点的和 解题报告
- [LeetCode]404. Sum of Left Leaves(叶子结点的和)
- leetcode_404. Sum of Left Leaves-左叶子的和
- 【LeetCode】Sum of Left Leaves 左叶子之和
- 404. Sum of Left Leaves | 所有左子树的和
- 404.leetcode Sum of Left Leaves(easy)[二叉树 递归]
- LeetCode 404. Sum of Left Leaves (二叉树,递归)
- LeetCode 404. Sum of Left Leaves 递归终止条件
- LeetCode笔记:404. Sum of Left Leaves
- 【leetcode】404. Sum of Left Leaves【E】
- LeetCode—404. Sum of Left Leaves
- [LeetCode]404. Sum of Left Leaves
- Leetcode 404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves
- 404. Sum of Left Leaves - leetcode
- 第七周项目4-队列数组
- UncaughtExceptionHandler 使用遇到的问题
- Java分页
- UIImageView的显示模式(contentMode属性)
- mysql #1170错误(42000) BLOB/TEXT Column Used in Key Specification Without a Key Length
- leetcode-404. Sum of Left Leaves 求左叶子节点的和,递归
- 第七周项目3——负数把正数赶出队列
- Linux下静态链接库与动态链接库的区别
- 第七周项目五 排队看病模拟
- setOnClickListener
- camel-exchange
- MySQL存储过程中的错误处理
- Leetcode 155 Min Stack
- 【第7周 项目3 - 顺序串算法】