题目链接

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 = Noneclass 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   

转载于:https://www.cnblogs.com/slurm/p/5272465.html

[leetcode] @python 113. Path Sum II相关推荐

  1. LeetCode OJ 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 ...

  2. [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 giv ...

  3. leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)

    题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...

  4. LeetCode 113. Path Sum II

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

  5. 113. Path Sum II

    /** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...

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

  7. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  8. 【DP】LeetCode 64. Minimum Path Sum

    LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...

  9. Path Sum II leetcode java

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  10. [Leetcode] 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 ...

最新文章

  1. 利用apache+svn+jenkins+maven 实现java环境的自动化构建和部署(三)(网内首发超详细版)...
  2. 分析 JDK 源码丨Java Thread
  3. 阿里三面,P9面试官是如何360°无死角考察候选人的?
  4. ACM训练小结-2018年6月19日
  5. python是c语言写的吗-C语言和python的区别
  6. Andriod anim rotate中的属性介绍
  7. Java提高篇——单例模式
  8. 机房突然发生爆炸事件如何处理?为什么会这样
  9. 操作系统中的进程与线程和java中的线程
  10. 2.14情人节,程序员该如何绝地反击?
  11. 扫地机器人开年之战:新品初现,战局微调
  12. mlock家族:锁定物理内存
  13. les有学计算机的吗,赵欢喜
  14. ubuntu 安装 virt-manager 虚拟机
  15. 构建之法阅读笔记三—结对编程
  16. 悬浮窗一个怎么够?微信新版本满足你的一心多用
  17. python三菱fx3u通讯_【案例】三菱FX3UPLC的无线通讯讲解
  18. 《七哥说道》第十三章:我的室友白羽鸡
  19. (附源码)计算机毕业设计SSM基于远程协作的汽车故障诊断系统
  20. 安装scss后报错Module build failed: TypeError: loaderContext.getResolve is not a function解决方法

热门文章

  1. andriod搭建自己的轮询框架
  2. HTML5 定位 —— Geolocation API的正确使用
  3. 美景听听Ai语音导游,助力华为荣耀PLAY手机发布
  4. 猫猫学iOS之小知识之xcode6自己主动提示图片插件 KSImageNamed的安装
  5. lua与c若干问题 - 专职C++ - C++博客
  6. 小卷积核的特征图与卷积参数变化不大,计算量变化明显
  7. 3月24 matlab函数polyfit(x,y,n)分析
  8. 七、数值微分与数值积分
  9. java jdk中优先队列的实现
  10. 图分析现状与探索落地