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]
]

path sum 是这个的基础,

path sum I是问存不存在这样一条路径,使得路径和为sum。使用递归。深度优先遍历的递归求法,见树的广度优先遍历和深度优先遍历(递归非递归、Java实现)
这题是要让求出这样的路径,也是同样地思路,使用递归,因为要添加到list中,所以每遍历一个就要往list中添加,直到叶子节点,判断是否等于sum,当等于,表明该路径符合,将list加到结果集中,同时别忘了,要删除叶子节点,再返回,进行下一个叶子判断。
如果不满足要求或者不是叶子节点,则将该节点加到list中,并继续遍历左右子树,遍历完了,也要删除该节点,返回上一层。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(root==null) return res;
helper(root,sum,res,new ArrayList<Integer>());
return res;
}
public void helper(TreeNode root,int sum,List<List<Integer>> res,List<Integer> list){
if(root==null) return; //使用这个,就不用再讨论左右子树谁为空了 if(root.left==null&&root.right==null&&root.val==sum){
list.add(root.val);
res.add(new ArrayList<Integer>(list));
list.remove(list.size()-1); //要将其删除再返回上一层
return ;
}else{
list.add(root.val);
helper(root.left,sum-root.val,res,list);
helper(root.right,sum-root.val,res,list);
list.remove(list.size()-1); //将其删除再返回上一层。
}
}
}

path sum II(深度优先的递归实现掌握)的更多相关文章

  1. Path Sum II深度优先找路径

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  2. 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 ...

  3. [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 ...

  4. 【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 ...

  5. 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 ...

  6. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

  7. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  8. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

  9. 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 ...

  10. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

随机推荐

  1. 开源项目——小Q聊天机器人V1.0

    小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...

  2. 未完成的IT路停在回车键---2014年末总结篇

    时间都去哪儿了?         一晃而过,越来越能体会到这个词的真实感.特别是过了二十岁,这种感觉越来越深刻,越来越强烈,犹如小编做公交车的时候一直向后排排倒的香樟树,还记得有首歌叫时间都哪儿了,而 ...

  3. LOV里的值直接引用系统里定义的值集的值,且具有值集的安全性控制

    fnd_flex_server.check_value_security(p_security_check_mode => 'YH', p_flex_value_set_id => p_f ...

  4. android sensor架构

    Android Sensor 架构深入剖析 作者:倪键树,华清远见嵌入式学院讲师. 1.Android sensor架构 Android4.0系统内置对传感器的支持达13种,它们分别是:加速度传感器 ...

  5. 利用编辑距离(Edit Distance)计算两个字符串的相似度

    利用编辑距离(Edit Distance)计算两个字符串的相似度 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可 ...

  6. MFC的两个问题

    1.启动WinApp的时候,报 ASSERT(AfxGetThread() == NULL)错误依赖的MFC DLL工程设置里面加上_USRDLL2. MFC误报内存泄露全局对象释放的问题,添加mfc ...

  7. UNIX环境高级编程——进程控制

    一.进程标识符 ID为0的进程是调度进程,常常被称为交换进程.该进程是内核的一部分,它并不执行任何磁盘上的程序,因此也被称为系统进程.进程ID 1通常是init进程,在自举过程结束时由内核调用.ini ...

  8. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十三)

    好了,现在在iOS模拟器中编译运行App,一切貌似都很好. 且慢,我们还没有到真机上调试呢?按说在编写App'时,无论如何应该尽快尽早在真机上调试.否则可能会碰到意想不到的问题,这次就是如此. 在真机 ...

  9. Mahout 算法

    Mahout 包括协同过滤,基于User和Item的推荐:kmeans.Fuzzy-kmeans .Mean shift .Dirichlet process .LDA聚类:奇异值分解:并行频繁项集挖 ...

  10. Errors running builder 'Integrated External Tool Builder' on project xxx

    出现这样的提示,表明你的项目的Builder项出了问题. 解决方法是: 右键项目选择"Properties",再选择"Builders",删除丢失的builde ...