LeetCode 113. 路径总和 II Python
2018-10-18 12:25
483 查看
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
给定如下二叉树,以及目标和
sum = 22,
5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1
返回:
[ [5,4,11,2], [5,8,4,5] ]
[code]# 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, sum1): res = [] if not root: return [] def dfs(root,path,mysum,sum1,res): if mysum == sum1 and not root.left and not root.right: res.append(path) return if root.left: dfs(root.left,path+[root.left.val],mysum+root.left.val,sum1,res) if root.right: dfs(root.right,path+[root.right.val],mysum+root.right.val,sum1,res) dfs(root,[root.val],root.val,sum1,res) return res
阅读更多
相关文章推荐
- Leetcode 113. 路径总和 II
- LeetCode-113.路径总和II(相关话题:深度优先)
- LeetCode 113. Path Sum II(路径和)
- [LeetCode]113. Path Sum II(列出二叉树根到叶路径和等于sum的所有路径)
- leetcode 113. Path Sum II-路径和|回溯算法
- leetcode 113. Path Sum II (路径和) 解题思路和方法
- leetcode 113. Path Sum II (路径和) 解题思路和方法
- [LeetCode] 113. Path Sum II 路径和 II
- [leetcode] @python 113. Path Sum II
- Leetcode 113. Path Sum II 路径和2 解题报告
- [Leetcode][python]Remove Duplicates from Sorted Array/Remove Duplicates from Sorted Array II
- Python [Leetcode 350]Intersection of Two Arrays II
- 59. Spiral Matrix II Leetcode Python
- leetcode 113. Path Sum II
- LeetCode119. Pascal's Triangle II-python(easy)
- 119. Pascal's triangle II Leetcode Python
- [leetcode]Palindrome Partitioning II @ Python
- [Leetcode][python]Linked List Cycle/Linked List Cycle II
- [leetcode]@python 81. Search in Rotated Sorted Array II
- Find Minimum in Rotated Sorted Array II Leetcode Python