[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.
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相关推荐
- 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 ...
- [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 ...
- 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/ 题解 简单的遍历二叉树,不解 ...
- 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 ...
- 113. Path Sum II
/** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...
- 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 ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- 【DP】LeetCode 64. Minimum Path Sum
LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...
- 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 ...
- [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 ...
最新文章
- 利用apache+svn+jenkins+maven 实现java环境的自动化构建和部署(三)(网内首发超详细版)...
- 分析 JDK 源码丨Java Thread
- 阿里三面,P9面试官是如何360°无死角考察候选人的?
- ACM训练小结-2018年6月19日
- python是c语言写的吗-C语言和python的区别
- Andriod anim rotate中的属性介绍
- Java提高篇——单例模式
- 机房突然发生爆炸事件如何处理?为什么会这样
- 操作系统中的进程与线程和java中的线程
- 2.14情人节,程序员该如何绝地反击?
- 扫地机器人开年之战:新品初现,战局微调
- mlock家族:锁定物理内存
- les有学计算机的吗,赵欢喜
- ubuntu 安装 virt-manager 虚拟机
- 构建之法阅读笔记三—结对编程
- 悬浮窗一个怎么够?微信新版本满足你的一心多用
- python三菱fx3u通讯_【案例】三菱FX3UPLC的无线通讯讲解
- 《七哥说道》第十三章:我的室友白羽鸡
- (附源码)计算机毕业设计SSM基于远程协作的汽车故障诊断系统
- 安装scss后报错Module build failed: TypeError: loaderContext.getResolve is not a function解决方法