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]
]
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(深度优先的递归实现掌握)的更多相关文章
- 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 ...
- 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]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】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: 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之“树”: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 ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- Path Sum II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...
- 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 ...
- 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 ...
随机推荐
- Java程序员的必备知识-类加载机制详解
类加载器的概念 类加载器是一个用来加载类文件的类. Java源代码通过javac编译器编译成类文件.然后JVM来执行类文件中的字节码来执行程序.类加载器负责加载文件系统.网络或其他来源的类文件. JV ...
- 开源项目——小Q聊天机器人V1.1
小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...
- Deploying Customizations in Oracle E-Business Suite Release 12.2
DeployingCustomizations in Oracle E-Business Suite Release 12.2 This documentdescribes how to deploy ...
- CUDA学习,环境配置和简单例子
根据摩尔定律,每18个月,硬件的速度翻一番.纵使CPU的主频会越来越高,但是其核数受到了极大的限制,目前来说,最多只有8个或者9个核.相比之下,GPU具有很大的优势,他有成千上万个核,能完成大规模的并 ...
- iOS中 FMDB第三方SQLite数据库 UI_20
1.什么是FMDB? FMDB是iOS平台下SQLite数据库,只不过它是OC方式封装了C语言的SQLite语句,使用起来更加面向对象 2.FMDB的优点:1.使用起来更加面向对象; 2.对比苹果自带 ...
- 【Unity Shaders】Using Textures for Effects —— 实现Photoshop的色阶效果
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- 自定义gradle的property
在android studio上运行项目,gradle的配置是必不可少的,但是随着项目的逐渐成长,迎面而来的就是.各种依赖包的添加,数不胜数的签名,渠道包等,整个gradle变得很乱,这样其实我们可以 ...
- 认识一下Android 事件分发机制
1.引子 由于android是采用分层布局(可以想象成PS时的图层概念一样),这样才可以在有限大小的手机屏幕上完成一些复杂的操作.当手指点击屏幕开始,这些动作在各层之间如何传递?就引出了Android ...
- 重构前VS重构后效果对比
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42554641 学习重构已经一个多月了,虽然不能让代码特别的 ...
- Android View底层到底是怎么绘制的
Android绘制链图: 网上很多讲Android view的绘制流程往往只讲到了Measure - Layout - Draw. 但是,这只是一个大体的流程,而我们需要探讨的是Android在我们 ...