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 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相关推荐
- [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 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 ...
- 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/ 题解 简单的遍历二叉树,不解 ...
- 113. Path Sum II
/** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 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 ...
- 【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 ...
- 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 ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- LeetCode 167.Two Sum II 解题报告
LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...
最新文章
- jQuery Mobile设置边距的宽度和颜色
- FFmpeg学习5:多线程播放视音频
- 递归求n的阶层算法实现
- 【以太坊开发】发币指南--进阶篇
- Dash for mac(代码文档浏览器)v6.0.8
- UGUI 优化关于图集,排序等
- TFS2010中文版下载
- 图解linux文件系统,linux系统的基本构成和文件系统的结构(配图解)
- linux 搜狗输入法包名,搜狗输入法
- Android实现自定义曲线截屏,Android实现矩形区域截屏的方法
- 什么是区块链(超详细)
- ubuntu 14.04 E450c 连不上网问题
- 经典同步时序逻辑电路分析汇总(第六道)(同步四进制可逆加减法计数器)
- 微信小程序开发架构——JavaScript的基本概述 和 JavaScript在 Nodejs、小程序中、浏览器中的使用方法
- 记一次个人服务器被nicehash挖矿,排查挖矿程序记录
- 操作系统_第五章文件管理_磁盘存储空间的管理
- 马云:不能把孩子放在温室里 光给孩子知识是不够的
- 两年聚37亿美元,“庞氏骗局” 维卡币负责人在美被捕
- Spring中的@Transactional(rollbackFor = Exception.class) try catch 异常时候 会失效
- 微软等数据结构+算法面试100题全部答案完整亮相