[leetcode] @python 113. Path Sum II
2016-03-13 17:02
681 查看
题目链接
https://leetcode.com/problems/path-sum-ii/题目原文
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:
Given the below binary tree and sum = 22,
return
题目大意
上一题的扩展版,返回满足所有符合条件的路径解题思路
使用dfs遍历代码
# 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 pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ def dfs(root, cursum, valuelist): if root.left == None and root.right == None: if cursum == sum: ans.append(valuelist) if root.left: dfs(root.left, cursum + root.left.val, valuelist + [root.left.val]) if root.right: dfs(root.right, cursum + root.right.val, valuelist + [root.right.val]) ans = [] if root == None: return [] dfs(root, root.val, [root.val]) return ans
相关文章推荐
- python安装scipy与sklearn模块
- [leetcode] @python 112. Path Sum
- Numpy 基础教程
- [leetcode]@python 111. Minimum Depth of Binary Tree
- 回归树-----生成回归树
- 用conda管理Python包
- win7下python3.4读写中文文件不乱码
- Python多线程中join函数与setDaemon函数使用说明
- Python多线程中join函数与setDaemon函数使用说明
- 简单讲解Python中的字符串与字符串的输入输出
- 100 numpy exercises
- NumPy的教程--数组,基本运算
- 深入解析Python中的list列表及其切片和迭代操作
- 我的python第一天
- Python里面的正则表达式:
- numpy常用函数
- Python爬虫
- Python爬虫
- Python爬虫
- Python爬虫