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 ...
随机推荐
- android文件混淆详解
-injars androidtest.jar[jar包所在地址] -outjars out[输出地址] -libraryjars 'D:\android-sdk-windows\plat ...
- springMVC源码分析--容器初始化(二)DispatcherServlet
在上一篇博客springMVC源码分析--容器初始化(一)中我们介绍了spring web初始化IOC容器的过程,springMVC作为spring项目中的子项目,其可以和spring web容器很好 ...
- 【自制插件】MMD4Maya
这个是之前MMD4MecanimImport的升级版,把pmx2fbx.exe整合了进来,不再需要Unity和MotionBuilder. 测试maya2015, maya2016可以用.maya20 ...
- 基于java自身技术实现消息方式的系统间通信
这篇博客基本照搬了分布式java应用基础与实践一书的内容 java自带的远程调用分两种一种是rmi,一种是webservice 我们先看rmi(remote method invoke)# 使用rmi ...
- Java Web 高性能开发,第 2 部分: 前端的高性能
Web 发展的速度让许多人叹为观止,层出不穷的组件.技术,只需要合理的组合.恰当的设置,就可以让 Web 程序性能不断飞跃.Web 的思想是通用的,它们也可以运用到 Java Web.这一系列的文章, ...
- 调用awk的三种方式
调用awk的三种方式 调用awk有三种方式,一种为Shell命令行方式,另外两种是将awk程序写入脚本文件,然后执行该脚本文件.三种方式的命令格式归纳如下: 一.在Shell命令行输入命令调用awk, ...
- Css技术入门笔记01
在学习html的时候,html中的标签都具备了特定功能,或者含义,以及相应的样式效果.可是在有些时候我们可能仅仅希望使用 html标签把要显示的数据封装起来,而不需要任何的样式效果.这时就需要单独的标 ...
- 《java入门第一季》之面向对象(形式参数和返回值问题的深入研究3)
/* 形式参数: 引用类型 接口:需要的是该接口的实现类对象 这个时候就没什么了,和抽象类的解释差不多. */ interface Love { public abstract ...
- Javascript和BHO的相互调用简介
v:* { } o:* { } w:* { } .shape { }p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-botto ...
- Process Order API - How To Scripts
In this Document Purpose Questions and Answers References APPLIES TO: Oracle Order Management ...