leetcode-404. Sum of Left Leaves 求左叶子节点的和,递归
2016-10-14 10:29
477 查看
题目:
Find the sum of all left leaves in a given binary tree.
Example:
题意:
求左叶子节点的和
代码:
# 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
Find the sum of all left leaves in a given binary tree.
Example:
3 / \ 9 20 / \ 15 7 There 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 404. Sum of Left Leaves 递归终止条件
- 404. Sum of Left Leaves求树的所有左叶子节点的和
- leetcode 404. Sum of Left Leaves 所有左孩子叶节点之和计算 + 深度优先遍历DFS
- LeetCode 404. Sum of Left Leaves (二叉树,递归)
- LeetCode-404. Sum of Left Leaves (Java)
- LeetCode 404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves
- 404. Sum of Left Leaves [LeetCode]
- 【LeetCode】Sum of Left Leaves 左叶子之和
- LeetCode 404. Sum of Left Leaves
- 【Leetcode】404. 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 404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves(二叉树)
- leetcode 404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves