题目

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

解题思路

忘了BFS和DFS的我,这道题有点蒙圈。贴一个介绍的博客基本算法——深度优先搜索(DFS)和广度优先搜索(BFS)

队列先进先出,用于BFS;栈后进先出,用于DFS。

BFS+queue

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:if not root:return []res = []queue = [(root, root.val, [root.val])]while queue:curr, val, ls = queue.pop(0) ## 先进先出,每一层都是先左后右。if not curr.left and not curr.right and val == sum:res.append(ls)if curr.left:queue.append((curr.left, val+curr.left.val, ls+[curr.left.val]))if curr.right:queue.append((curr.right, val+curr.right.val, ls+[curr.right.val]))return res

DFS+stack

class Solution:def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:if root is None:return []res = []stack = [(root, root.val, [root.val])]while stack:curr, val, ls = stack.pop()if not curr.left and not curr.right and val == sum:res.append(ls)if curr.right:stack.append((curr.right, val+curr.right.val, ls+[curr.right.val]))if curr.left:stack.append((curr.left, val+curr.left.val, ls+[curr.left.val]))return res

Leetcode: 113. Path Sum II相关推荐

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

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

  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. 113. Path Sum II

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

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

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

  7. LeetCode OJ - Path Sum II

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

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

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

  9. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

最新文章

  1. jQuery Mobile设置边距的宽度和颜色
  2. FFmpeg学习5:多线程播放视音频
  3. 递归求n的阶层算法实现
  4. 【以太坊开发】发币指南--进阶篇
  5. Dash for mac(代码文档浏览器)v6.0.8
  6. UGUI 优化关于图集,排序等
  7. TFS2010中文版下载
  8. 图解linux文件系统,linux系统的基本构成和文件系统的结构(配图解)
  9. linux 搜狗输入法包名,搜狗输入法
  10. Android实现自定义曲线截屏,Android实现矩形区域截屏的方法
  11. 什么是区块链(超详细)
  12. ubuntu 14.04 E450c 连不上网问题
  13. 经典同步时序逻辑电路分析汇总(第六道)(同步四进制可逆加减法计数器)
  14. 微信小程序开发架构——JavaScript的基本概述 和 JavaScript在 Nodejs、小程序中、浏览器中的使用方法
  15. 记一次个人服务器被nicehash挖矿,排查挖矿程序记录
  16. 操作系统_第五章文件管理_磁盘存储空间的管理
  17. 马云:不能把孩子放在温室里 光给孩子知识是不够的
  18. 两年聚37亿美元,“庞氏骗局” 维卡币负责人在美被捕
  19. Spring中的@Transactional(rollbackFor = Exception.class) try catch 异常时候 会失效
  20. 微软等数据结构+算法面试100题全部答案完整亮相

热门文章

  1. mongodb集合的增删
  2. 使用FPM快速生成RPM包
  3. Silverlight动态设置WCF服务Endpoint
  4. table和div在页面布局上应该注意的问题
  5. FreeBSD最小化安装没有man解决方法
  6. yolo-mask的损失函数l包含三部分_损失函数总结-应用和trick
  7. 什么是CNN卷积神经网络的感受野及动画演示
  8. 关于numpy中eye和identity的区别详解
  9. ORACLE_SID含义
  10. mysql ssl 登陆_MySQL:SSL远程登录