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.
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一脉同源。仅仅是改变了下题目的描写叙述。详细思路是用回溯法,将所有的节点所有遍历。
详细思路和代码例如以下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* 回溯法求解
* 总体思想是遍历。然后加入list逐一试探
* 符合要求的加入结果集
* 不符合要求的删除,然后回溯
*/
List<List<Integer>> list = new ArrayList<List<Integer>>();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if(root == null){
return list;
}
path(root,sum,new ArrayList<Integer>());
return list;
} private void path(TreeNode root,int sum, List<Integer> al){
if(root == null){
return;
}
if(root.val == sum){
if(root.left == null && root.right == null){
al.add(root.val);
//加入结果一定要又一次生成实例
list.add(new ArrayList<Integer>(al));
al.remove(al.size()-1);//删除
return;
}
}
al.add(root.val);
path(root.left,sum - root.val,al);
path(root.right,sum - root.val,al);
al.remove(al.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 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- [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 ...
- Java for 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 (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
随机推荐
- Git版本号控制
Git是分布式版本号控制系统.与SVN类似的集中化版本号控制系统相比.集中化版本号控制系统尽管可以令多个团队成员一起协作开发,但有时假设中央server宕机的话,谁也无法在宕机期间提交更新和协 ...
- jsapi微信支付v3版
请看清楚你的微信支付是v2还是v3.在这里整理的是v3的,v2的同学请忽略! 前期准备须要用的是商户证书,用的是p12的.设置api密钥(在微信商户端中设置),还须要在微信公众号中设置jsapi授权文 ...
- Spark Tachyon的命令行使用
Tachyon命令行使用 Tachyon接口说明 接口操作示例 copyFromLocal copyToLocal ls和lsr count cat mkdir.rm.rmr和touch pin和un ...
- storm1.1运行时问题
java.lang.NoClassDefFoundError: org/apache/curator/shaded/com/google/common/cache/CacheBuilder 程序并没有 ...
- Nim游戏算法实现
- FZU 1608 Huge Mission
Huge Mission Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original I ...
- hdoj-1289-A Bug's Life【种类并查集】
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- jquery--this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- js19--继承终极版本
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Android开发经验之点击图片判断是否在图片范围之内
package xiaosi.grivaty; import android.content.Context; import android.graphics.Bitmap; import andro ...