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++ ...
随机推荐
- React开发实时聊天招聘工具 -第六章 登陆注册(2)
1.bodyParser和cookieParser: const bodyParser = require('body-parser') const cookieParser = require( ...
- 一个Web报表项目的性能分析和优化实践(五):重构有助于性能优化么?
项目从初次开发到现在,已经快3年了.期间,有N个工程师参与过. 需求方面:增加减少,反反复复,无数次:人力方面:增加减少,不稳定:时间方面:功能开发着急上线,Bug开发紧急修复. 因此,代码臃肿,问题 ...
- ognl.OgnlException: target is null for setProperty(null,"XXXX"...)
今天遇到了这个奇葩问题,最后来回比对了一下前辈写过的一段完整代码后才发现问题. Error大概描写叙述为: 警告: Error setting expression 'XXX' with value ...
- 怎样借助log4j把日志写入数据库中
log4j是一个优秀的开源日志记录项目.我们不仅能够对输出的日志的格式自定义,还能够自定义日志输出的目的地,比方:屏幕.文本文件,数据 库,甚至能通过socket输出.本节使用MySQ ...
- hdu 1171 Big Event in HDU(01背包)
代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; in ...
- android framework 01
.(由下向上启动),Uboot引导内核(linux Kernel)启动,把内核从flash放到内存中,引导内核启动.内核是系统的核心,负责进程的管理内存的管理网络的管理.内核(Linux Kenel) ...
- python爬虫之『入门基础』
HTTP请求 1.首先需要了解一下http请求,当用户在地址栏中输入网址,发送网络请求的过程是什么? 可以参考我之前学习的时候转载的一篇文章一次完整的HTTP事务过程–超详细 2.还需要了解一下htt ...
- 关于IDEA编译器在初次使用thymeleaf 引入无效 , 导致th无法使用的原因
首先pom.xml里面要导入thymeleaf的依赖 然后在html中加入 xmlns:th="http://www.thymeleaf.org" 最后点击file ---> ...
- 【hihocoder 1378】网络流二·最大流最小割定理
[Link]:http://hihocoder.com/problemset/problem/1378 [Description] [Solution] 在求完最小割(最大流)之后; 可以在剩余网络中 ...
- Java中Thread源码剖析
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 关于线程,用很长时间了,主线程下的子线程去做一些事情,就是一个代理模式,主线程分代理权给子线程,子线 ...