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 ...
随机推荐
- 15个实用find命令
除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易. 本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令. 1. 用文 ...
- java设计模式(三)
单例模式在一个jvm中有且仅有一个对象(1)内部静态类实现 class Singleton{ /*构造方法私有 防止实例化*/ private Singleton(){}; public static ...
- C语言 · 数的统计
问题描述 在一个有限的正整数序列中,有些数会多次重复出现在这个序列中. 如序列:3,1,2,1,5,1,2.其中1就出现3次,2出现2次,3出现1 次,5出现1次. 你的任务是对于给定的正整数序列,从 ...
- BUAA 2014级数据结构第五次上机 二叉树之数组转换广义表
按题意建立好二叉树,再按照先序遍历输出结果. #include<cstdio> #include<vector> #include<queue> #include& ...
- Struts2-2.了解struts.xml>package>action>result的name属性
result决定跳转到哪个视图(jsp),可以预设值有多个. <?xml version="1.0" encoding="UTF-8" ?> < ...
- [JS思路]运动框架思路
匀速运动的思路一: 1.先清除动画,再加载动画 2.方向dir有正值和负值,可以通过 目标值 > 当前值 往右移动,即正数 目标值 < 当前值 往右移动,即负数 来进行判断:dir = ...
- JS计算字符串长度(中文算2个)
/** * @return {number} */ getRealLength = function(str) { var realLength = 0, len = str.length, char ...
- webapi中的Route的标签的命名参数name的使用
Route Names In Web API, every route has a name. Route names are useful for generating links, so that ...
- web小技巧
如内容超出单元格,则隐藏style="TABLE-LAYOUT: fixed" 让弹出窗口总是在最上面: <body onblur="this.focus();&q ...
- WTL版本ACEdit控件,改写自MFC版,附带源码
自动完成是个很酷也很实用的功能,比如在浏览器地址栏输入几个字母,相关的记录就会在下拉框中陈列出来. 最近在做公司产品UI部分的改善,原版本是MFC做的,我决定用WTL,于是就遇到自动完成控件的问题.遍 ...