[LeetCode]题解(python):113 Path Sum II

题目来源



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.



题意分析



Input: a binary tree, sum

Output: list of list.

Conditions: 给定一个二叉树,将所有root-leaf的路径值和等于sum的路径返回。



题目思路



通过一个valuelist传递。dfs递归



AC代码(Python)

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def pathSum(self, root, sum):
10         """
11         :type root: TreeNode
12         :type sum: int
13         :rtype: List[List[int]]
14         """
15         def dfs(root, currsum, valuelist):
16             if root.left == None and root.right == None:
17                 if currsum == sum: res.append(valuelist)
18             if root.left:
19                 dfs(root.left, currsum+root.left.val, valuelist+[root.left.val])
20             if root.right:
21                 dfs(root.right, currsum+root.right.val,valuelist+[root.right.val])
22
23         res = []
24         if root ==None: return res
25         dfs(root, root.val, [root.val])
26         return res
时间: 2024-10-16 12:53:37

[LeetCode]题解(python):113 Path Sum II的相关文章

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium 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, 5 / 4 8

【一天一道LeetCode】#113. Path Sum II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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,

LeetCode 113. Path Sum II 20170705 部分之前做了没写的题目

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, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题目大意:给定一棵二叉树和一个总和,找出所有从根节点

LeetCode 113. Path Sum II路径总和 II (C++)

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 Return: [ [5,4,11

113. Path Sum II (Tree; DFS)

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, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] struct TreeNode { int val

LeetCode 113: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, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] Subscribe to see which co

leetcode 113 Path Sum II ----- java

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, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 上一道题的延伸,求出所有结果. /** * Defi

Java for LeetCode 113 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, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 解题思路: DFS,JAVA实现如下: stati

leetcode 113 path Sum II 路径和

递归先序遍历+vector<int>容器记录路径 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12