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 ...
随机推荐
- php灌水
<?php $data=array('name'=>'paul','mail'=>'paulversion@163.com') $data=http_build_query($dat ...
- ActionBar之style出现Cannot resolve symbol 'Theme' 错误
今天 2014/03/08 00:49 刚刚升级 android studio 到了 0.5.0 版本,修复了许多 bug,包含当前这个问题,之前一直困扰我很久,莫名奇妙的提示主题样式找不到,无法解析 ...
- zoj 1649 Rescue
BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...
- Android Paint Xfermode 学习小结
一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的"过渡 ...
- WCF初接触实作
我们通过实现一个简单的示例来对WCF有个直观而浅显的认识,希望对初次涉及WCF的朋友有所帮助. 可以简单地认为WCF程序分为4部分:契约.服务.宿主.客户端.我们通过一个例子来逐步完成各部分,示例程序 ...
- spring slf4j log4j maven
参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview.html <depen ...
- Tomcat 配置支持APR
对ARP支持,需要安装以下库: APR library JNI wrappers for APR used by Tomcat (libtcnative) OpenSSL libraries 其中JN ...
- redis :初步使用
redis : 1.ubuntu安装 'pip install redis-server' 2.启动 'redis-cli' 3.使用 set: set a 1 get: get a fl ...
- WebSocket的原理,以及和Http的关系
一.WebSocket是HTML5中的协议,支持持久连接:而Http协议不支持持久连接. 首先HTMl5指的是一系列新的API,或者说新规范,新技术.WebSocket是HTML5中新协议.新API. ...
- sqlite 学习
到谷歌上搜sqlite,第一项便是官方网站:www.sqlite.org.进去后,先了解一下大体,感觉还不错. 进入Document页面,大标题SQLite Programming Interface ...