113. Path Sum II
/** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无* 2.可选的范围:每一个node的左边和右边子节点(不为空的情况)。* 3.往前走一步:temp加一个,sum减一个* 4.后退一步:把temp remove掉* 5.特殊的case:root==null也就是走到了根节点的下一个空了* 这个题目别看答案简单,自己做起来千疮百孔了:* 1.dfs函数以前习惯一上来就是成功案例的base case,殊不知万一root为null的情况根本就没有下文了* 2.需要一开始就把temp加上root的值,因为万一遇到只有一个节点1,和sum=1,应该返回new ArrayList<Integer>(temp)* 总结就是注意空节点的判断和左右节点分别为空的判断才能走下一步*/public List<List<Integer>> pathSum(TreeNode root, int sum) {List<List<Integer>> res = new ArrayList<List<Integer>>();List<Integer> temp = new ArrayList<Integer>();dfs(root, sum, res, temp);return res;}public void dfs(TreeNode root, int sum, List<List<Integer>> res, List<Integer> temp){if(root==null) return;temp.add(root.val);if(root.left==null && root.right==null ){if(root.val==sum)res.add(new ArrayList<Integer>(temp));return;}if(root.left!=null) {dfs(root.left,sum-root.val,res,temp);temp.remove(temp.size()-1);}if(root.right!=null) {dfs(root.right,sum-root.val,res,temp);temp.remove(temp.size()-1);}}
转载于:https://www.cnblogs.com/zmyvszk/p/5510858.html
113. Path Sum II相关推荐
- [LeetCode]113.Path Sum II
[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the giv ...
- leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)
题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...
- LeetCode 113. Path Sum II
113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...
- Leetcode: 113. Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode OJ 113. 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: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- Path Sum II leetcode java
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- [Leetcode] 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. 路径总和 II(Path Sum II)
目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及 ...
最新文章
- wxWidgets:wxTaskBarIcon 演示
- RabbitMQ消息重复消费问题
- 传128GB版iPad4售价为799/929美元
- Java import static静态导入
- 软件测试之手动测试存在的重要原因
- Anaconda使用pyinstaller打包exe程序体积过大
- 已收藏!java面试知识点整理
- 手机游戏行业洗牌在即 成本增长小团队出局
- 如何解决macbook开机密码输入正确,却提示“密码错误”无法登陆?
- 基于ssh人事考勤管理系统
- VMware 安装windows XP SP3 镜像下载地址 回忆xp经典
- 入门月薪8k,3年年薪35w,大数据的就业前景与薪酬待遇浅析
- Spectral–spatial hyperspectral image classification
- 华为加班夜宵报销要主管审批?任正非怒斥:多此一举
- 浅析-微服务3搭建框架
- 使用redis所维护的代理池抓取微信文章
- 增加平均订单价值 – 交叉销售VS.升级销售
- linux 文件夹 775,linux - 如何为文件夹及其所有子文件夹和文件设置chmod?
- 全球光刻机龙头是怎样炼成的
- Linux 系统黑洞 /dev/null
热门文章
- vue切换路由页面数据缓存_Vue-Router实现前端页面缓存
- ListCtrl::DeleteColumn的一点小经验
- C专家编程—分析signal函数的原型声明{void (*signal(int sig,void (*func)(int)))(int)}(2)
- UBoot讲解和实践-----------讲解(一)
- IPython Notebook简介1
- 贪心/动态规划 - 摆动序列
- 3.调用empty而不是检查size()是否为0
- 记录 之 遇到的 lamda 表达式和功能理解
- Tensorflow保存模型详解(进阶版二):如何保存最近的.ckpt文件 及 如何分开保存.ckpt数据文件和.meta图文件
- 怎样进入服务器修改东西,进入服务器修改数据库