Path Sum II——LeetCode
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,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
题目大意:给一个二叉树和一个数,求从根节点到叶结点的和等于这个数的所有路径。
解题思路:DFS,首先把当前节点入栈,然后分别递归左右子树,如果当前节点为空直接退出,如果当前节点是叶结点但是到根节点的路径和不等于指定的数也直接退出,如果等于指定的数,那么把这条路径加入结果List,递归完之后回退到上一层。
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
List<Integer> tmp = new ArrayList<>();
path(root, tmp, res, sum);
return res;
}
public void path(TreeNode node, List<Integer> tmp, List<List<Integer>> res, int sum) {
if (node == null) {
return;
}
if (sum != node.val && node.left == null && node.right == null) {
return;
}
tmp.add(node.val);
if (sum == node.val && node.left == null && node.right == null) {
res.add(new ArrayList<>(tmp));
tmp.remove(tmp.size() - 1);
return;
}
path(node.left, tmp, res, sum - node.val);
path(node.right, tmp, res, sum - node.val);
tmp.remove(tmp.size() - 1);
}
Path Sum II——LeetCode的更多相关文章
- Path Sum II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...
- 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 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- [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 ...
- 【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 ...
随机推荐
- MYSQL 体系结构图 log commit
- qt 程序启动参数 -qws
运行嵌入式程序 在嵌入式QT版本中,程序需要服务器或自己作为服务器程序.服务器程序构造的方法是构造一个QApplication::GuiServe类型的QApplication对象.或者使用-qws命 ...
- Swift: 下标(Subscripts)
类.结构体.枚举都可以定义下标(subscript),下标是访问集合.列表.序列的元素的快捷方式. 在Swift中可以为类型定义下标,而且不限于一维. 语法 下标定义的方法:跟实例方法的语法类似,su ...
- [转] 有趣的JavaScript原生数组函数
在JavaScript中,可以通过两种方式创建数组,Array构造函数和 [] 便捷方式, 其中后者为首选方法.数组对象继承自Object.prototype,对数组执行typeof操作符返回‘obj ...
- IP 转地址
1.需要 QQWry.Dat IP 地址数据库 2辅助类库 using System; using System.Collections.Generic; using System.IO; usin ...
- 玩转iOS 9的UIDynamics(转)
转自 http://www.cocoachina.com/ios/20150716/12613.html 本文由CocoaChina翻译小组成员AGSpider(微博)翻译自fancypixel的博客 ...
- windows下安装CI框架
CI框架是一个非常流行的 mvc框架, CI框架如何安装和使用,在CI中文网已经讲的比较详细了 ,这里记录下几个需要注意的地方. 一. index.php问题 把压缩包下载解压到项目根目录即可运行里面 ...
- WebService笔记-Schema约束
Schema约束 俗话说得好 好记性不如烂笔头. 看了下WebService视频,觉得还是得下笔记. 观看的视频地址:http://edu.51cto.com/ ...
- javescript扩展方法
<script type="text/javascript"> //扩展方法 '原型'->'prototype' //通过类对像的prototype设置扩展方法 ...
- (git fetch git push git pull)远程本地分支互相推送更新
git push origin bug_huiyuan:mobile_attribution 把bug_huiyuan(本地分支) 推送到 远程mobile_attribution分支 git pus ...