LeetCode #404: Sum of Left Leaves
来源:互联网 发布:阿里云合作伙伴 logo 编辑:程序博客网 时间:2024/06/05 04:47
Problem Statement
(Source) 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.
Solution
这道题有很明显的递归结构。
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def helper(self, root): if not root: return 0 elif not root.left and not root.right: return root.val else: return self.sumOfLeftLeaves(root) def sumOfLeftLeaves(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 else: return self.helper(root.left) + self.sumOfLeftLeaves(root.right)
1 0
- 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
- LeetCode #404: Sum of Left Leaves
- LeetCode 404 Sum of Left Leaves (DFS)
- leetcode 404 Sum of Left Leaves
- LeetCode #404 - Sum of Left Leaves - Easy
- leetcode 404 Sum of Left Leaves C++
- 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
- Sum of Left Leaves ---LeetCode
- Leetcode Sum of Left Leaves
- LEETCODE--Sum of Left Leaves
- springMVC注解中@RequestMapping中常用参数value params 以及@RequestParam 详解
- WSGI
- 用java实现希尔排序
- 泳道图好用
- Linux命令之"ping"
- LeetCode #404: Sum of Left Leaves
- POJ 1700 Crossing River 过河问题 贪心法
- Android中的广播详解
- (OK) MPTCP - 100% OK
- 交叉编译器arm-linux-gcc
- android网络请求get方式
- 有用的函数集(持续更新)
- Spring框架的AOP/IOC/容器核心原理实例详解
- HDU 2955