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. android viewStub

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 开发应用的时候,需要根据条件决定显示某个视图, 这个时候可以用ViewStub Stub ...

  2. 在Eclipse中修改web项目的名称

    在Eclipse中修改web项目的名称 一.误区: 单击要修改名称的项目上右键Refactor->Rename,然后修改成另外一个名称 (光这样是不够的,哪怕你再修改web.xml中的displ ...

  3. 【并查集&&带权并查集】BZOJ3296&&POJ1182

    bzoj1529[POI2005]ska Piggy banks [题目大意] n头奶牛m种语言,每种奶牛分别掌握一些语言.问至少再让奶牛多学多少种语言,才能使得它们能够直接或间接交流? [思路] ( ...

  4. [HDU6155]Subsequence Count

    题目大意: 给定一个01序列,支持以下两种操作: 1.区间反转: 2.区间求不同的子序列数量. 思路: 首先我们考虑区间反转,这是一个经典的线段树操作. 接下来考虑求不同的子序列数量,在已知当前区间的 ...

  5. Problem D: 深入浅出学算法005-数7

    Description 逢年过节,三五好友,相约小聚,酒过三旬,围桌数七. “数七”是一个酒桌上玩的小游戏.就是按照顺序,某人报一个10以下的数字,然后后面的人依次在原来的数字上加1,并喊出来,当然如 ...

  6. 【对比分析二】Web Storage和cookie的区别

    1)  存储空间不同. a)  Web Storage能提供5MB的存储空间(不同浏览器的提供的空间不同).Cookie仅4KB. b)  Web Storage每个域(包括子域)有独立的存储空间,各 ...

  7. SMACH专题(一)----安装与初探

    最近使用ROS进行任务(Task)执行,深切体会用传统的方法实现是极其繁杂的.比如人脸录入工作,包含人脸检测,识别,语音提示,运动控制,这些子部分基本都是通过订阅话题的回调函数中处理,之间的切换,如人 ...

  8. 【原】使用Spring自带的JdbcTemplate。

    使用Spring自带的JdbcTemplate,可以简化对数据库的操作,用起来十分方便.通过一下几个步骤的配置,即可以使用JdbcTemplate. (1)配置好Spring的数据源,加入mysql驱 ...

  9. POJ 2104 && POJ 2761 (静态区间第k大,主席树)

    查询区间第K大,而且没有修改. 使用划分树是可以做的. 作为主席树的入门题,感觉太神奇了,Orz /* *********************************************** ...

  10. 线程系列06,通过CLR代码查看线程池及其线程

    在"线程系列04,传递数据给线程,线程命名,线程异常处理,线程池"中,我们已经知道,每个进程都有一个线程池.可以通过TPL,ThreadPool.QueueUserWorkItem ...