Path Sum II——LeetCode
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]
]
题目大意:给一个二叉树和一个数,求从根节点到叶结点的和等于这个数的所有路径。
解题思路:DFS,首先把当前节点入栈,然后分别递归左右子树,如果当前节点为空直接退出,如果当前节点是叶结点但是到根节点的路径和不等于指定的数也直接退出,如果等于指定的数,那么把这条路径加入结果List,递归完之后回退到上一层。
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
List<Integer> tmp = new ArrayList<>();
path(root, tmp, res, sum);
return res;
} public void path(TreeNode node, List<Integer> tmp, List<List<Integer>> res, int sum) {
if (node == null) {
return;
}
if (sum != node.val && node.left == null && node.right == null) {
return;
}
tmp.add(node.val);
if (sum == node.val && node.left == null && node.right == null) {
res.add(new ArrayList<>(tmp));
tmp.remove(tmp.size() - 1);
return;
}
path(node.left, tmp, res, sum - node.val);
path(node.right, tmp, res, sum - node.val);
tmp.remove(tmp.size() - 1);
}
Path Sum II——LeetCode的更多相关文章
- Path Sum II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...
- 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 笔记 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] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- 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 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- [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 ...
随机推荐
- 【小TIP】记录各种错误【更新中】
最好程序一遍通过,为了提高代码能力,这里将用TIP的形式记录来犯过的错误.不断更新中. *已经转移到闪存.. [150214]WA:检查是否数组开小了. [150212]WA:如果程序中有乘号,需要留 ...
- 请输出in.txt文件中的2 4 6 8 9 10 12行
in.txt文件: 学号 姓名 性别 年龄 1001 张三 男 18 1002 赵四 男 19 1003 李丽 女 18 1004 刘芳 女 32 1005 王五 男 54 1006 小明 男 32 ...
- codevs 1515 跳
/* 画矩阵找规律发现是杨辉三角 Cg (i,j)= C (i+j,i); 贪心走的话 沿着0行(列)一直走然后拐直角 拐弯后每个格子都累加 Cg (n,0) + Cg (n,1) + Cg (n,2 ...
- LINQ的基本用法
1.var q =from c in db.Customers select c.ContactName; 这个语句只是一个声明或者一个描述,并没有真正把数据取出来,只有当你需要该数据的时候,它才会执 ...
- 应用app首次进入导航页动画
import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActi ...
- 《第一行代码》学习笔记1-Android系统架构
1. 2003.10,Andy Rubin创办Android公司.2005.8,Google收购之,并于2008年推出Android系统第一个版本. 2. ①Linux Kernel:基于Linux ...
- 使用WebUploader使用,及使用后测试横拍或竖拍图片图片方向不对等解决方案
WebUploader是由Baidu WebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件.在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流IE浏览 ...
- 网络编程Socket之UDP
服务器端实现步骤: 1. 创建 DatagramSocket,指定端口号 2. 创建 DatagramPacket 3. 接收客户端发送的数据信息 4. 读取数据 package cn.jmu.edu ...
- jqgrid设置单元格数据
$("#gridid").jqGrid('setCell',rowid,icol,data); rowid为行ID,jqgrid内置的那个,从1开始 icol为列索引,从0开始, ...
- java-二分查找法
package search; public class BinarySearch { public static void main(String[] args) { , , , , , , , , ...