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. JVM CPU占满问题定位

    RASP加载后出现JVM CPU占满问题,jstack -F输出信息无法找到对应占用CPU的线程 perf定位到占用CPU的热代码位于Dependencies::find_finalizable_su ...

  2. Javascript和JQuery之间的联系

    一.Js对象和Jquery对象之间的相互转化1.Js对象转化为Jquery对象 var p = document.getElementById('p'); var $obj = $(p); 2.Jqu ...

  3. hdu 1514 记忆化搜索

    题意是给4堆(堆的高度小于等于40)有颜色(颜色的种类小于等于20)的物品,你有一个篮子最多能装5件物品,每次从这4堆物品里面任取一件物品放进篮子里,但是取每堆物品时,必须先取上面的物品,才能取下面的 ...

  4. LeetCode:整数反转(Reserve Integer)

    public class ReserveInteger { public int reverse(int x) { //用于接收个位数(10的余数) int remainder; //是否负数 int ...

  5. bzoj 4176: Lucas的数论 -- 杜教筛,莫比乌斯反演

    4176: Lucas的数论 Time Limit: 30 Sec  Memory Limit: 256 MB Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么 ...

  6. 洛谷OJ U552 守墓人 线段树模板题

    题目描述 Description 在一个荒凉的墓地上 有一个令人尊敬的守墓人, 他看守的墓地从来 没有被盗过, 所以人们很放心的把自己的先人的墓 安顿在他那 守墓人能看好这片墓地是必然而不是偶然... ...

  7. C# 传统的ToString

    C# 传统的ToString DataRow dr=item; var str=dr["Name"]; str.ToString();//dr["Name"]= ...

  8. ROS知识(14)----局部避障的动态窗口算法(DWA)及其调试的方法

    Dynamic Window Approach(DWA)是重要的局部轨迹规划算法,ROS中使用了DWA算法获得了很好的局部路径规划的效果.具体的教程可参考官方的导航调试资料Navigation Tun ...

  9. AbstractAction

    package cn.tz.action.abs; import java.io.File; import java.io.IOException; import java.text.SimpleDa ...

  10. 事件冒泡 比bubble

    冒泡的概念就是 当子元素触发事件的时候 相应的祖宗十八代素也会触发相同的事件(前提父元素也添加了一样的事件)eg:儿子 有一个onclick 祖宗十八代 也有onclick 当点击儿子的时候 祖宗十八 ...