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 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]
]
相比较判断一个树中是否有sum为某一值的从根节点到叶子节点的路径,这个题目要求把所有这样的路径找出来。如果我们要用递归的方法,如何来存储这样的路径呢?
一个节点如果有左右两个子节点,那么就会产生两条不同的路径,在递归的过程中,我们要不断地new一个list来存储新出现的分支,并要把从根节点到达当前节点的路径拷贝到新建的list中。当到达叶子节点后,如果是一条匹配路径,则把该路径加入到存储路径的list中,否则的话舍弃该路径。代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution { public List<List<Integer>> list = new ArrayList();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if(root == null) return list; List<Integer> llist = new ArrayList();
path(root, sum, llist);
return list;
} public void path(TreeNode root, int sum, List<Integer> llist){
if(root.left==null && root.right==null){
if(root.val==sum){
llist.add(sum);
list.add(llist);
}
return;
}
if(root.right!=null){
List<Integer> rlist = new ArrayList();
for(Integer i : llist){
rlist.add(i);
}
rlist.add(root.val);
path(root.right, sum - root.val, rlist);
}
if(root.left!=null){
llist.add(root.val);
path(root.left, sum - root.val, llist);
}
}
}
LeetCode OJ 113. Path Sum II的更多相关文章
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 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 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】113. 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 OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- LeetCode OJ: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 ...
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
随机推荐
- ANSII 与Unicode,Utf8之间的转换
在项目开发中,我们难免会遇到各种问题,特别是字符直接的转换,这里列举字符直间转换的代码: using namespace std; wstring AnsiiToUnicode(const strin ...
- Android Studio环境下代码混淆+签名打包
Android Studio环境下代码混淆+签名打包 作者 Mr_冯先生 关注 2016.08.21 01:10 字数 1040 阅读 734评论 5喜欢 34 注:本文使用的Android Stud ...
- SQLAlchemy入门
什么是SQLAlchemy? 是Python连接SQL数据库的一种方式,需要通过驱动来连接. 是Python中使用最广泛的ORM(对象关系映射)工具之一,即把数据库中的二维表结构映射成一个list对象 ...
- MVC中的Startup.Auth.cs、BundleConfig.cs、FilterConfig.cs和RouteConfig.cs
一.MVC中的Startup.Auth.cs.BundleConfig.cs.FilterConfig.cs和RouteConfig.cs四个文件在app_start中 <1>Bundle ...
- iOS开发自定义流水布局
//集成UICollectionViewFlowLayout 自己写的布局 // SJBFlowLayout.m // 自定义流水布局 // // Created by zyyt on 16/7 ...
- Mac下svn的使用
1.从本地导入代码到服务器(第一次初始化导入) 在终端中输入 svn import /Users/apple/Documents/eclipse_workspace/weibo svn://local ...
- 模拟器SDK路径
I'm guessing you already find it out, but just for the record: the UIKit.framework is available only ...
- 不创建类将json数据转换
一般,取到json数据之后,都会将json数据转换为对象,通过属性取得里面的属性值,这样做可以很好地利用vs的智能提示,让开发更轻松,但是代价就是,你需要手动的创建json数据相对应的类. 也有其他方 ...
- 洛谷-语文成绩-[有奖]洛谷5月月赛:kkksc03的三大神器
题目背景 Background语文考试结束了,成绩还是一如既往地有问题. 题目描述 Description语文老师总是写错成绩,所以当她修改成绩的时候,总是累得不行.她总是要一遍遍地给某些同学增加分数 ...
- redis数据类型:lists
redis的list类型其实就是一个每个子元素都是string类型的双向链表. 我们可以通过push,pop操作从链表的头部或者尾部添加删除元素,这样list即可以作为 栈,又可以作为队列. lpus ...