[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 sum.
Note: A leaf is a node with no children.
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]
]
题意:
二叉树之和,返回所有和等于给定值的路径
思路:
要穷举所有路径。 backtracking。
思路类似two sum, 通过(sum - 当前值) 来check 叶子节点的值是否与一路作减法的sum值相等
代码:
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> result = new ArrayList<>();
ArrayList<Integer> cur = new ArrayList<>(); // 中间结果
helper(root, sum, cur, result);
return result;
} private static void helper(TreeNode root, int sum, ArrayList<Integer> cur,
List<List<Integer>> result) {
if (root == null) return; cur.add(root.val); // leaf node
if (root.left == null && root.right == null) {
if (sum == root.val)
result.add(new ArrayList<>(cur));
} helper(root.left, sum - root.val, cur, result);
helper(root.right, sum - root.val, cur, result); cur.remove(cur.size() - 1);
}
}
[leetcode]113. Path Sum II路径和(返回路径)的更多相关文章
- [LeetCode] 113. Path Sum II 路径和 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路径总和 II (C++)
题目: 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 (路径和) 解题思路和方法
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 路径和
递归先序遍历+vector<int>容器记录路径 /** * Definition for a binary tree node. * struct TreeNode { * int va ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [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 su ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- 113 Path Sum II 路径总和 II
给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22, 5 / \ 4 ...
随机推荐
- [Windows]Win10下VM虚拟机桥接模式无法上网的解决办法
Win10出来了,赶紧尝尝鲜.既然是预览版,肯定会出现以前没有过的问题.这不,问题马上就来了.我的VM虚拟机本来在Win8.1下使用桥接模式是可以上网的,但是现在不可以了.重置了好几次虚拟网络,NAT ...
- Python:23种Pandas核心操作
Pandas 是一个 Python 软件库,它提供了大量能使我们快速便捷地处理数据的函数和方法.一般而言,Pandas 是使 Python 成为强大而高效的数据分析环境的重要因素之一.在本文中,作者从 ...
- mysql更新(五) 完整性约束 外键的变种 三种关系 数据的增删改
11-数据的增删改 本节重点: 插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 再来回顾一下之前我们练过的一些操作,相信大家都对插入数据.更新数据.删除数据有了全面的认识. ...
- metaq架构原理
原创文章,转载请注明出处:http://jameswxx.iteye.com/blog/2034111 本来只是想看下metaq的文档,结果发现好乱,现在metaq其实有两个大分支了,一个是庄晓丹 ...
- linux操作系统5 shell编程
知识内容: 1.shell编程预备知识 2.shell变量 3.表达式与运算符 4.分支循环语句 5.函数 一.shell编程预备知识 1.什么是shell编程 shell是与linux交互的基本工具 ...
- 《opencv学习》 之 几何变换
图像平移: 1.不改变图像大小 2.改变图像大小 编程按照目标图像的角度去编写 不改变大小的平移 1 void imageTranslation1(Mat& src, Mat& dst ...
- UVA-755-排序
奇怪,我怎么还有一个排序题目没过 题意如下: 公司喜欢有难忘的电话号码,一个让电话号码变得难忘的方式是有一个拼读起来难忘的单词,比如,你可以呼叫University of Waterloo通过拨打难忘 ...
- UVA439-水题
题意:一只棋盘上的马,从一个点到另外一个点要走多少步 解法:广搜 #include<stdio.h> #include<iostream> #include <strst ...
- ganglia
A.lamp界面快速搭建---------------------------------------------------------------------------------------- ...
- Firemonkey 绘图 TPathData
Firemonkey TPathData TPath控件 procedure TForm12.FormPaint( Sender : TObject; Canvas : TCanvas; const ...