[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)
# 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的更多相关文章
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 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 ...
- 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 ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- 【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 ...
- [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 ...
- [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 ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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 ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
随机推荐
- MongoDB 入门之基础 DDL
此文章主要记录部分主要的 MongoDB 的 DDL 操作. db 查看当前所在的数据库(默认 test) > db test > show dbs 查看当前数据库服务器上的数据库名字 ...
- datagrid点击标题进行排序
步骤: 1.页面上首先设置datagrid的AllowSorting="true",以及指定排序方法OnSortCommand="DataGrid1_SortComman ...
- [Word]将word文件中的软回车符[↓]替换为硬回车符
Ctrl+H,替换对话框 搜索:^l 替换:^p 确定替换即可.
- ccc 正态分布
cc.Class({ extends: cc.Component, properties: { prefab: { default:null, type:cc.Prefab }, root: { de ...
- Storm on Yarn :原理分析+平台搭建
Storm on YARN: Storm on YARN被视为大规模Web应用与传统企业应用之间的桥梁.它将Storm事件处理平台与YARN(Yet Another Resource Negotiat ...
- [笔记] Duke - 统计预测
Duke大学富卡商学院(Fuqua school of business)的高级选修课. 全名:Statistical forecasting: notes on regression and tim ...
- HDU 2836 (离散化DP+区间优化)
Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...
- 为OpenResty增加ngx_pagespeed模块进行优化
1.下载ngx_pagespeed模块 wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.8.31.4-beta.zip unzip ...
- ACM +-字符串
+-字符串 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 Shiva得到了两个只有加号和减号的字符串,字串长度相同.Shiva一次可以把一个加号和它相邻的减号交换. ...
- 匈牙利 算法&模板
匈牙利 算法 一. 算法简介 匈牙利算法是由匈牙利数学家Edmonds于1965年提出.该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. 二分图的定义: 设G=(V,E)是一个 ...