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. Visual Studio的ASP.NET修改默认打开浏览器

    1.新建web空应用程序,输入项目名称. 2.右击项目名称,添加“新建项”—一般处理程序 3.右击新建一个HTML文件,任意命名. 4.右击新建的Html文件,选择“浏览方式” 5.选择后弹出浏览器选 ...

  2. webservice主流框架Axis、Axis2、XFire、CXF的比较

    http://ws.apache.org/axis/ http://axis.apache.org/axis2/java/core/ http://xfire.codehaus.org/ http:/ ...

  3. Struts2中获取HttpServletRequest,HttpSession等的几种方式

    转自:http://www.kaifajie.cn/struts/8944.html package com.log; import java.io.IOException; import java. ...

  4. DB2 VC++ 中连接字符串

    根据你安装的驱动,有如下两种连接字符串形式.Provider=DB2OLEDB;Network Transport Library=TCPIP;Network Address=xxx.xxx.xxx. ...

  5. GPL协议

    GPL-General Public License 它是自由软件许可(Free Software Licence),保证最终使用者能够自由的引用,学习,拷贝,甚至修改软件.在理解这之前需要了解什么是 ...

  6. 17.1.2?Replication Formats 复制格式:

    17.1.2?Replication Formats 复制格式: 17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Ba ...

  7. VS2010/MFC设置对话框控件的Tab顺序

    设置对话框控件的Tab顺序 前面几节为大家演示了加法计算器程序完整的编写过程,本节主要讲对话框上控件的Tab顺序如何调整. 上一讲为“计算”按钮添加了消息处理函数后,加法计算器已经能够进行浮点数的加法 ...

  8. 算法导论 6.5.9 堆实现K路归并问题

    问题: 设计一个时间复杂度为O(NlogK)的算法,它能够将K个有序链表合并为一个有序链表,这里的N为所有输入链表包含的总的元素个数 分析: 该问题为经典的利用堆完成K路归并的问题: 当K个序列满足一 ...

  9. 双网卡绑定(suse)

    网卡绑定技术有助于保证高可用性特性并提供其它优势以提高网络性能,Linux双网卡绑定实现就是使用两块网卡虚拟成为一块网卡,这个聚合起来的设备看起来是一个单独的以太网接口设备,就是两块网卡具有相同的IP ...

  10. EJB3.0开发环境的搭建

    EJB Container的介绍SUN公司正式推出了EJB的规范之后,在众多的公司和开发者中引起了非常大的反响.标志着用Java开发企业级应用系统将变的非常easy.很多公司都已经推出了或正打算EJB ...