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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/path-sum-ii/description/

题目描述:

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,2],
[5,8,4,5]
]

题目大意

在一棵二叉树中,找出从根节点到叶子节点的和为target的所有路径。

解题方法

其实一看这个题就能看出来这个是经典的回溯法的题目。看来是我手生了,竟然一下没写出来。

我卡在的地方在于回溯的时候root的处理方式,最后有下面两种处理方式吧。我更倾向于第二种,先把root给添加上去再回溯。

# 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]]
"""
res = []
self.dfs(root, sum, res, [])
return res def dfs(self, root, target, res, path):
if not root: return
path += [root.val]
if sum(path) == target and not root.left and not root.right:
res.append(path[:])
return
if root.left:
self.dfs(root.left, target, res, path[:])
if root.right:
self.dfs(root.right, target, res, path[:])
path.pop(-1)

换了一种做法,貌似看起来更简单直白了。

# 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]]
"""
if not root: return []
res = []
self.dfs(root, sum, res, [root.val])
return res def dfs(self, root, target, res, path):
if not root: return
if sum(path) == target and not root.left and not root.right:
res.append(path)
return
if root.left:
self.dfs(root.left, target, res, path + [root.left.val])
if root.right:
self.dfs(root.right, target, res, path + [root.right.val])

日期

2018 年 6 月 22 日 ———— 这周的糟心事终于完了

【LeetCode】113. Path Sum II 解题报告(Python)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 变量、内存区域、MDK文件(map、htm)

    变量分为:局部变量和全局变量 局部变量:函数体内部定义的变量,作用域为函数内部,static声明(静态局部变量)该变量则函数调用结束后不消失而保留值,分配的存储空间不释放. 全局变量:函数体外部定义的 ...

  2. 码上来战!探索“智”感生活,HMS Core线上Codelabs挑战赛第4期开始!

    HMS Core线上Codelabs挑战赛第4期正式开始!我们向所有实践力超强.创新力满满的开发者发出邀请,用你的超级"码"力,解锁更多应用价值! 生活里,我们被手机"秒 ...

  3. 表格table的宽度问题

    首先注意table的一个样式 table { table-layout:fixed; } table-layout有以下取值: automatic 默认.列宽度由单元格内容设定 fixed 列宽由表格 ...

  4. linux系统中tomcat的安装及使用

    linux系统中tomcat的安装及使用 linux系统中安装tomcat tar.gz/tar文件格式安装 先下载好该文件,将文件放置在校安装的目录下, 如果是tar.gz后缀使用 tar -zxv ...

  5. 强化学习实战 | 表格型Q-Learning玩井字棋(一)

    在 强化学习实战 | 自定义Gym环境之井子棋 中,我们构建了一个井字棋环境,并进行了测试.接下来我们可以使用各种强化学习方法训练agent出棋,其中比较简单的是Q学习,Q即Q(S, a),是状态动作 ...

  6. Learning Spark中文版--第六章--Spark高级编程(1)

    Introduction(介绍) 本章介绍了之前章节没有涵盖的高级Spark编程特性.我们介绍两种类型的共享变量:用来聚合信息的累加器和能有效分配较大值的广播变量.基于对RDD现有的transform ...

  7. 隐藏状态栏后tableview自动上移20个像素的问题

    最近在开发过程中碰到一个很奇怪的问题,将状态栏隐藏掉之后,页面上的tableView会自动上移20个像素. 这是因为在iOS7.0之后,系统会自动调整scrollView的layout 和 conte ...

  8. 3.3 rust HashMap

    The type HashMap<K, V> stores a mapping of keys of type K to values of type V. It does this vi ...

  9. Dubbo中CompletableFuture异步调用

    使用Future实现异步调用,对于无需获取返回值的操作来说不存在问题,但消费者若需要获取到最终的异步执行结果,则会出现问题:消费者在使用Future的get()方法获取返回值时被阻塞.为了解决这个问题 ...

  10. yaml 配置文件的语法。

    1.基本语法 1. k:(空格)v:表示一对键值对(注意:空格必须有): 2.以**空格**的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的 3.值的驼峰写法和用"-" ...