Path Sum II 总结DFS
https://oj.leetcode.com/problems/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 很类似,在他的基础上,要求输出所有和为sum的path。上题讲过,这种题目一般用DFS来做,那么在递归DFS的时候,同时维护一个总的结果的List<List<Integer>> resultList,和一个当前可能的子结果List<Integer> currentList。
这道题本该一次性就写出来,无奈犯了一个非常低级的语言层面的错误。符合条件的时候,直接将currentList加入到resultList中了。这样resultList中等于是存了N个一模一样的list对象了。应该每次拷贝currenList生成一个新的对象放入resultList,就不会有问题了。代码如下:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
List<Integer> currentList = new ArrayList<Integer>();
DFS(root, sum, 0, resultList, currentList);
return resultList;
} public void DFS(TreeNode root, int sum, int sumCurrent, List<List<Integer>> resultList, List<Integer> currentList){
if(root == null){
return;
} currentList.add(root.val);
sumCurrent = sumCurrent + root.val;
if(root.left == null && root.right == null){
if(sumCurrent == sum){
//之前一直错,犯了低级错误!!!resultList.add(currentList);
resultList.add(new LinkedList(currentList));
}
}
DFS(root.left, sum, sumCurrent, resultList, currentList);
DFS(root.right, sum, sumCurrent, resultList, currentList);
currentList.remove(currentList.size() - 1);
//下面一行可要可不要
// sumCurrent = sumCurrent - root.val;
}
}
可是至少有一个问题,为什么同样是子状态,前面currentList就一定要回溯,currentSum就不要回溯?
上面的算法中,我们看到一个DFS+回溯算法的基本模样,前面提到的另外两道题DFS题目,Letter Combinations of a Phone Number 和 Word Break II 也都是这个模型。可以说,这种算法无论是在面试中还是实际编程中都是相当重要的,理解了它会有种豁然开朗的感觉。我们可以稍微总结一下。
首先,判断递归返回的条件,写在最开始。然后,处理当前节点。处理完当前节点后,DFS下一节点,最后回溯。或者循环处理当前节点内的所有可能的状态(比如上面电话号码簿里一个数字所有代表的字母,这时一般用for循环)。
回溯是这个算法的关键,下面我借用一个网友的伪代码,讲讲自己对上面的问题,也就是回溯,为什么要回溯的理解。引用的网址在下面有。
void dfs(int 当前状态)
{
if(当前状态为边界状态)
{
记录或输出
return;
}
for(i=0;i<n;i++) //横向遍历解答树所有子节点
{
//扩展出一个子状态。
修改了全局变量
if(子状态满足约束条件)
{
dfs(子状态)
}
恢复全局变量//回溯部分
}
}
从上面的伪代码里我们可以看到,对当前节点的一个状态的操作,往往会包含对全局变量的修改,类似于本题中的currentList。想想递归的过程,实际上是一个树形的占空间,每个子状态都有自己的栈,存放当前的临时变量,这也就是为什么有时候递归非常耗费空间的原因。
这时我们再来看上面提出的问题,为什么同样是子状态,前面currentList就一定要回溯,currentSum就不要回溯?这里我只说Java,因为自己对C或者C++不了解。
在Java里,所有方法(或函数)的参数,只有pass by value,这和C++可以传引用是非常不同的。所以所有的primitive type,比如int, char等,传的都是值。在本题中,DFS方法的参数,sum和sumCurrent传的都是他们的数值,而不是本身,他们都是每个递归状态都会维护在自己占空间内的,所以后面的修改不会影响前面栈内的值。这就是currentSum不需要回溯的原因。用上面的伪代码来说,它不是全局变量。最后一行sumCurrent = sumCurrent - root.val;是多余的,因为它减也是减去当前栈内的值,和其他栈并不相关。
反过来,currenList是一个对象。严格来说,Java中,currentList中存的是一个内存地址,这片内存内才住着真正的对象。所以DFS方法传参的时候,pass by value,就将这个地址传了进去。在整个递归的过程中,所有对currenList的修改,都是修改的这一个内存地址,也就是这一个对象。各自自状态的栈空间里,保存的副本也仅仅是这个内存地址的值。各个递归状态对currenList的修改,都会影响其他状态。这也就是为什么currentList需要回溯的原因。dfs修改后,需要立刻恢复它到修改前的状态。
有其他网友总结的更好,我来转载下。
http://blog.csdn.net/fightforyourdream/article/details/12866861
http://blog.chinaunix.net/uid-26602509-id-3178938.html
http://blog.sina.com.cn/s/blog_779fba530100wqz9.html
http://blog.csdn.net/cdfr2321388/article/details/5725863
从这个问题上,我们还能比较好的理解Java里pass by value的实质,为什么primitive就是pass by value,而object type却感觉是pass by reference,实际上也是pass by value。
那么思路来了,我们能不能将这个currenList在每次递归的栈空间里都维护一个副本,这样就不需要回溯了嘛。下面的代码便是这样的一个实现。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
List<Integer> currentList = new ArrayList<Integer>();
DFS(root, sum, 0, resultList, currentList);
return resultList;
} public void DFS(TreeNode root, int sum, int sumCurrent, List<List<Integer>> resultList, List<Integer> currentList){
if(root == null){
return;
} //回溯的关键,每次申明一个新的内存放当前状态的结果currentList,后面就不要回溯了
List<Integer> soFarList = new LinkedList(currentList);
soFarList.add(root.val);
sumCurrent = sumCurrent + root.val;
if(root.left == null && root.right == null){
if(sumCurrent == sum){
//之前一直错,犯了低级错误!!!resultList.add(currentList);
resultList.add(new LinkedList(soFarList));
}
}
DFS(root.left, sum, sumCurrent, resultList, soFarList);
DFS(root.right, sum, sumCurrent, resultList, soFarList);
//下面一步就不必要了,这个对理解回溯很重要
// currentList.remove(currentList.size() - 1);
//下面一行可要可不要
// sumCurrent = sumCurrent - root.val;
}
}
应该说,回溯放在递归方法的最后面,理解起来是有点难度的,这样一解释应该能清楚很多。
这类题目在面试中经常出现,以及DFS和BFS的区别,啥时候用啥,各自优缺点之类,需要好好理解。
Path Sum II 总结DFS的更多相关文章
- 113. Path Sum II (Tree; DFS)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- LeetCode Path Sum II (DFS)
题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...
- 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] 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搜索到新节点时,都要保存该节点.而且每当找出一 ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
随机推荐
- (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn rever
import java.util.ArrayList; import java.util.List; public class AA { public static void main(String[ ...
- C# 获取所有网卡信息
private void Form1_Load(object sender, EventArgs e) { //获取说有网卡信息 NetworkInterface[] nics = NetworkIn ...
- 如何修改yii2.0用户登录使用的user表为其它的表
这只是自己练习的一个记录而已. 因为某种原因,不想用yii自带的user表,想用自己建的admin数据库表,修改如下: 1. 参考高级模板里里的common\models\User 修改 Admi ...
- PCL:描述三维离散点的ROPS特征(Code)
前言: 三维点云为三维欧式空间点的集合.对点云的形状描述若使用局部特征,则可分为两种:固定世界坐标系的局部描述和寻找局部主方向的局部描述,ROPS特征为寻找局部主方向的特征描述. 1.寻找主方向(对X ...
- (转)基于Metronic的Bootstrap开发框架经验总结(3)--下拉列表Select2插件的使用
http://www.cnblogs.com/wuhuacong/p/4761637.html 在上篇<基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JST ...
- js判断数组中是否包含某个值
/** * 判断数组中是否包含某个值 * @param arr 数组 * @param str 值 * @returns {boolean} */ function contains(arr, str ...
- javaee Properties键值对写入和读取方法
package Zjshuchu; import java.util.Properties; import java.util.Set; public class Demo03 { public st ...
- [POI2005]SKA-Piggy Banks tarjan 水题
Code: #include<bits/stdc++.h> #define maxn 1000002 using namespace std; void setIO(string s) { ...
- BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd
Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...
- Django基础(二)
Django基础(二) http://www.cnblogs.com/wupeiqi/articles/4508271.html