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 ...
随机推荐
- app如何更换用户头像信息呢?不妨这样做
对于现在的手机应用而言,要想获得更多的人的使用,就需要给用户更多的自由功能才行,这也是基于用户体验开发软件的核心思想,一切以用户为中心,想用户之所想,做用户之所需.今天我就来谈一谈刚学到的一个关于设置 ...
- Android:android sdk源码中怎么没有httpclient的源码了
欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 今天想使用这个API,怎么也找不到.废了好多时间... 查阅资料才知道如下解释: 在android 6.0(API 23)中,G ...
- java设计模式---三种工厂模式
工厂模式提供创建对象的接口. 工厂模式分为三类:简单工厂模式(Simple Factory), 工厂方法模式(Factory Method)和抽象工厂模式(Abstract Factory).GOF在 ...
- EBS DBA指南笔记(二)
第三章 监控和诊断 本章涵盖以下几个主题:监测的方法,数据库的监测,apache的监测,forms的监测,并发管理器的监测,服务器的监测,网络的监测,其它的一些监测和诊断方法. 1.监测的方法:主 ...
- 【一天一道LeetCode】#71. Simplify Path
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- UIScrollView UIPageViewControlle…
1.UIScorollView 是ios中提供的滑动控件,用来解决当内容区域大于scorollView可视区域时,可以通过滑动的方式查看整个内容区域,UIScorollView 的滑动控件的基类 ...
- Dynamics CRM2013 1:N关系 sub-grid中的“添加现有项”和“添加新建项”功能详解
CRM2013中sub-grid的样式和2011中有了较大的变化,2013和2011界面对比如下 在2011的时候按钮是在ribbon区,1:N的父子关系实体直接点击添加新纪录就可以,但2013就不行 ...
- MySQL学习笔记_7_MySQL常用内置函数
MySQL常用内置函数 说明: 1)可以用在SELECT/UPDATE/DELETE中,及where,orderby,having中 2)在函数里将字段名作为参数,变量的值就是字段所对应的每一行的值. ...
- EBS 外部信用风险检查
DECLARE l_msg_count NUMBER; l_msg_data VARCHAR2(2000); l_return_status VARCHAR2(30); l_cc_hold_comme ...
- (NO.00001)iOS游戏SpeedBoy Lite成形记(十一)
之前的10篇内容主要实现了选手从起点移动至终点的动作,比较随机的模拟了选手的速度变化,另外完成了选手到达终点时该做的事情. 接下来的几篇中我们进一步完善SpeedBoy Lite项目,使它真正成为一个 ...