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 ...
随机推荐
- Spring MVC(一)
MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域,MVC特指一种表现层设计模式,不限于Java语言 spring mvc属于spring框架的后续产品,用在基于MVC的表现层开发,类 ...
- 部分服务器使用phpExcel会报错
其中一个错误提示是:Fatal error: 'break' not in the 'loop' or 'switch' context in /var/www/htdocs/hanya/ThinkP ...
- 关于安装第三方模块和PILLOW
看廖雪峰老师这一节的教程卡在这里挺久了 在谷歌上了搜了很久,最后根据这个教程上解决了这个问题 http://www.yihaomen.com/article/python/566.htm 觉得自己好蠢 ...
- Android设置对话框去除黑边
在res/values/styles.xml文件中添加如下内容 <style name="dialog" parent="@android:style/Theme. ...
- Ctrl+Alt+T恢复启动Ubuntu默认终端
对于Ubuntu14.04,如果安装了terminator,那么快捷键Ctrl+Alt+T将不会启动自带的terminal,而是启动安装的terminator,如果想恢复回来,可以执行以下命令: su ...
- 【LeetCode】463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- Java IO 理解流的概念
Java IO 理解流的概念 @author ixenos 在理解流时首先理解以下概念 1.流的来源和去向一般在构造器指出 2.方法中的形参一般是将流输出到某个位置,读取(INPUT)流从流读出数据( ...
- C# 非public的方法和属性的单元测试
有时候我们写好的类库中,某些类的属性和方法不应该暴露出来,那么如何对这些非public的方法和属性进行单元测试? MS为我们提供了PrivateObject类,可以解决这个问题,可以去MSDN的说明文 ...
- createThread和_beginthreadex区别
摘自:http://blog.csdn.net/morewindows/article/details/7421759 CreateThread()函数是Windows提供的API接口,在C/C++语 ...
- spring之json数据的接受和发送
配置spring对json的注解方式. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springf ...