[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 ...
随机推荐
- [UE4]场景光照改进PostProcessVolume
PostProcessVolume可以做的效果很多,其中就可以实现太阳光斑效果. Unbound勾上上,就表示不受“PostProcessVolume”组件的大小限制,直接应用到整个世界.如果不勾选, ...
- Unreal Engine 4 笔记 2
转自:http://blog.csdn.net/st_dark/article/details/48005947 2.Actor继承自aactor,可以看成是一个容器,用来装"组件" ...
- ORA-03113:通信通道的文件结尾
问题: 用命令startup启动实例时,报错“ORA-03113:通信通道的文件结尾”. 解决: SQL> startup mount ORACLE 例程已经启动. Total System G ...
- 探究CSS中border-top属性的使用
上一节我们介绍了CSS top属性的用法,那么这节关于CSS border-top属性用法学习起来就会轻松一些,border-top 简写属性把上边框的所有属性设置到一个声明中. 本文向大家描述一下C ...
- tcp协议和udp协议的使用场景
一:什么是TCP(Transmission Control Protocol,传输控制协议) tcp是面向连接的协议,也就是说,在收发数据前,必须和对方建立可靠的连接.一个TCP连接必须要经过三次 ...
- PHP写日志公共类
Txl_Log.php <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * * * ...
- python入门-异常
1 报错的例子 print(5/0) 2跳过报错的例子 try: print(5/0) except ZeroDivisionError: print("You can't divide b ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 263: i llegal multibyte sequence
UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 263: illegal multibyte seq ...
- 65. sqlserver执行存储过程实例
declare @param varchar(500)exec sp_PUB_GetFlowStatus @ret output,10011,88,1,12print @ret
- idea中spring boot启动后无法访问jsp
出自:https://www.jianshu.com/p/470b28d76147 第一种: 打开File > Project Structure > Facetes 如图1: 图1 如果 ...