题目来源


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)

 # 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, currsum, valuelist):
if root.left == None and root.right == None:
if currsum == sum: res.append(valuelist)
if root.left:
dfs(root.left, currsum+root.left.val, valuelist+[root.left.val])
if root.right:
dfs(root.right, currsum+root.right.val,valuelist+[root.right.val]) res = []
if root ==None: return res
dfs(root, root.val, [root.val])
return res

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

  1. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  2. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  3. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  4. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  5. 【LeetCode】113. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  6. [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 su ...

  7. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

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

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

  9. 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 su ...

  10. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

随机推荐

  1. DrawingContext.Pop Method

    The following example shows the effect of the Pop command. using System; using System.Windows; using ...

  2. 安装SQL2008 提示 创建usersettings/microsoft.sqlserver.configuration.landingpage.properties.se

    System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServer.Configuration ...

  3. CF# 334 Moodular Arithmetic

    B. Moodular Arithmetic time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. 20145308刘昊阳 《Java程序设计》第3周学习总结

    20145308刘昊阳 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 要产生对象必须先定义类,类是对象的设计图,对象是类的实例 类是从少数实例推广到大量相似实例的 ...

  5. [小工具]ChemistryHelper

    // 此博文为迁移而来,写于2015年3月25日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vuv7.html 前几天 ...

  6. Windows Phone 8 LongListSelector实现下拉自动刷新列表

    LongListSelector是一个加强版的列表控件,它可以实现分组的列表,如系统人脉列表的交互效果就可以利用LongListSelector控件去实现,同时LongListSelector也一样可 ...

  7. jsp页面价格

    <div class='flo_left'> <h2 class='red'>订货价¥${productEntity.agentsPrice}</h2> <h ...

  8. 【Go语言】集合与文件操作

    本文目录 1.数据集合的主要操作 1_1.字典的声明 1_2.字典的初始化和创建 1_3.字典的访问和操作 1_4.其他类型的数据集 2.文件操作 2_1.文件操作概述os包和path包 2_2.文件 ...

  9. android中versionCode&versionName

    原文来自:http://blog.csdn.net/wh_19910525/article/details/8660416 ,略有修改 一.概述 Android的版本可以在androidmainfes ...

  10. hadoop配置远程客户端

    独立出一台机器,作为客户端,可以连接远程集群,配置注意事项: 1.首先是hive,需要服务器启动一个服务 hive --service metastore 然后修改hive客户端 hive-site. ...