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

Discuss

java code : 
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(root == null)
return res;
ArrayList<Integer> tmp = new ArrayList<Integer>();
recursion(root, res, tmp, sum);
tmp = null;
return res;
}
public void recursion(TreeNode root, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> tmp, int sum)
{
if(root.left == null && root.right == null)
{
tmp.add(root.val);
int sum1 = 0;
for(int i = 0; i < tmp.size(); i++)
{
sum1 += tmp.get(i);
}
if(sum1 == sum)
res.add(new ArrayList<Integer>(tmp));
return ;
}
tmp.add(root.val);
if(root.left != null)
{
recursion(root.left, res, tmp, sum);
tmp.remove(tmp.size() - 1);
}
if(root.right != null)
{
recursion(root.right, res, tmp, sum);
tmp.remove(tmp.size() - 1);
}
}
}

【LeetCode】Path Sum II 二叉树递归的更多相关文章

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

  2. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  3. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  4. [LeetCode] Path Sum IV 二叉树的路径和之四

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

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

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

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

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

  9. [leetcode]Path Sum II @ Python

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

随机推荐

  1. bs和cs

    CS(Client/Server):客户端----服务器结构.C/S结构在技术上很成熟,它的主要特点是交互性强.具有安全的存取模式.网络通信量低.响应速度快.利于处理大量数据.因为客户端要负责绝大多数 ...

  2. [ 转载 ] Java基础12--基础学习总结——数组

    java基础学习总结——数组 一.数组的基本概念 数组可以看成是多个相同类型数据组合,对这些数据的统一管理. 数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量. 数组的 ...

  3. 解读socketserver之Tcpserver

    在解析socketserver是如工作之前,我们先看看socektserver类的继承关系图: 请求类继承关系: server类继承关系: 有了上面的继承关系图后,我们解析socketserver就轻 ...

  4. redis keys 命令

    ## 删除存在的key del key ## 序列体弱给定key,并返回被序列化的值 dump key ## 检查key是否存在 exists key ## 为给定key设置过期时间 expire k ...

  5. MyBatis之MyBatis环境搭建

    MyBatis之MyBatis环境搭建 一.MyBatis开发环境搭建 1.引入Jar包 ①MyBatis mybatis-3.4.1.jar ant-1.9.6.jar ant-launcher-1 ...

  6. [POI2000]病毒 --- AC自动机

    [POI2000]病毒 题目描述: 二进制病毒审查委员会最近发现了如下的规律: 某些确定的二进制串是病毒的代码. 如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的. 现在委员会已经找 ...

  7. 在Kali Linux上编译Windows EXP

    使用vc6.0去编译的时候,难免会出现点问题 这里找到MS11-046的exp来编译 poc地址:https://www.exploit-db.com/exploits/40564/ 首先需要安装mi ...

  8. ASP.NET 构建高性能网站 架构设计

    Web前端系统 为了达到不同应用的服务器共享.避免单点故障.集中管理.统一配置等目的,不以应用划分服 务器,而是将所有服务器做统一使用,每台服务器都可以对多个应用提供服务,当某些应用访问量升高时,通过 ...

  9. 怎么样退出vi/vim编辑器

    怎么样退出vi/vim编辑器 先按   ESC   然后输入  w  q  :wq 就退出来了

  10. git push时提示"fatal: The current branch master has no..."

    git push到远程仓库时提示:fatal: The current branch master2 has no upstream branch. To push the current branc ...