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]
]

给定一个二叉树和一个值。找出全部根到叶的路径和等于这个值的路径。

深度优先遍历。

	public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
dfs(root,sum,ret,list);
return ret;
}
public void dfs(TreeNode root,int sum,List<List<Integer>> ret,List<Integer> list){
if(root == null)
return ;
if(root.val == sum && root.left == null && root.right == null){
list.add(root.val);
List<Integer> temp = new ArrayList<Integer>(list);//拷贝一份
ret.add(temp);
list.remove(list.size() - 1);//再删除
return ;
}
list.add(root.val);
dfs(root.left,sum-root.val,ret,list);
dfs(root.right,sum-root.val,ret,list);
list.remove(list.size() - 1);
}
// Definition for binary tree
public class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}

LeetCode——Path Sum II的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [LeetCode] 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 ...

  4. [Leetcode] 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 ...

  5. [leetcode]Path Sum II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

  6. leetcode: 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 ...

  7. [LeetCode] 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 ...

  8. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  9. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

随机推荐

  1. http 代理 测试

    Technorati 标记: http 代理验证及测试 Technorati 标记: C# 参考了网上很多资料,综合整理出来最终的代码:   using System; using System.Co ...

  2. 8.PHP 教程_PHP字符串

    字符串变量用于存储并处理文本. PHP中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量中. 在下面的实例中, ...

  3. cocos2d-x spine 加载粒子特效

    spine骨骼加载粒子效果 将粒子效果加到骨骼上,update位置和角度.h#ifndef __HOMESCENE_H__#define __HOMESCENE_H__#include "c ...

  4. (Problem 41)Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  5. 在GridView中实现全选反选的例子

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView控件.asp ...

  6. python开发_大小写转换,首字母大写,去除特殊字符

    这篇blog主要是总结我们在平常开发过程中对字符串的一些操作: #字母大小写转换 #首字母转大写 #去除字符串中特殊字符(如:'_','.',',',';'),然后再把去除后的字符串连接起来 #去除' ...

  7. 用Mediawiki做百科网站资源大参考

    MediaWiki简易安装教程**关于mediawiki 一些好的资料: http://codex.wordpress.org.cn/Mediawiki%E5%BB%BA%E7%AB%99%E7%BB ...

  8. POJ-1010 Stamps

    [题目描述] 题目大意是:邮票发行商会发行不同面值.不同种类的邮票给集邮爱好者,集邮爱好者有总目标面额,通过不同的邮票组合(总数在4张以内)达到该面值,卖给集邮爱好者.另外,发行商发行的邮票面值最多2 ...

  9. 启动及重新启动nginx,重启nginx后丢失nginx.pid问题解决

    停止操作 停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的 步骤1:查询nginx主进程号 ps -ef | grep nginx 在进程列表里 面找master进程 ...

  10. PHP中用到的一些字符串函数

    /*常用的字符串输出函数   *   * echo()   输出字符串   * print()  输出一个或多个字符串   * die()  输出一条信息,并退出当前脚本   * printf()  ...