题目

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]
] 题解
这道题除了要判断是否有这样的一个path sum,还需要把所有的都可能性结果都返回,所以就用传统的DFS递归解决子问题。代码如下:
 1     public void pathSumHelper(TreeNode root, int sum, List <Integer> sumlist, List<List<Integer>> pathlist){
 2         if(root==null) 
 3             return;
 4         sumlist.add(root.val);
 5         sum = sum-root.val;
 6         if(root.left==null && root.right==null){
 7             if(sum==0){
 8                 pathlist.add(new ArrayList<Integer>(sumlist));
 9             }
         }else{
             if(root.left!=null)
                 pathSumHelper(root.left,sum,sumlist,pathlist);
             if(root.right!=null)
                 pathSumHelper(root.right,sum,sumlist,pathlist);
         }
         sumlist.remove(sumlist.size()-1);
     }
     
     public List<List<Integer>> pathSum(TreeNode root, int sum) {
         List<List<Integer>> pathlist=new ArrayList<List<Integer>>();
         List<Integer> sumlist = new ArrayList<Integer>();
         pathSumHelper(root,sum,sumlist,pathlist);
         return pathlist;
     }
												

Path Sum II leetcode java的更多相关文章

  1. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

  2. Path Sum II——LeetCode

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  3. Combination Sum II leetcode java

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

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

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

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  7. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  8. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

  9. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

随机推荐

  1. android 上下边框线

    <!-- 连框颜色值 --> <item> <shape> <solid android:color="@android:color/backgro ...

  2. Activator 动态构造对象

    Activator  意义: 用于动态构造对象 语法1: 根据指定的泛型类型构造对象 Activator.CreateInstance<类型>() 语法2: 根据程序集和类型名构造对象 S ...

  3. 初始化collectionViewCell

    #import <UIKit/UIKit.h> @interface TonyCollectionViewCell : UICollectionViewCell @property UII ...

  4. ThreadLocal 详解

    什么是ThreadLocal 根据JDK文档中的解释:ThreadLocal的作用是提供线程内的局部变量,这种变量在多线程环境下访问时能够保证各个线程里变量的独立性. 从这里可以看出,引入Thread ...

  5. MongoDB的Java驱动使用整理 (转)

    MongoDB Java Driver 简单操作 一.Java驱动一致性 MongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,Mongo有个内置的连接池(池大小默认为 ...

  6. 在eclipse中查看Android源码

    声明:高手跳过此文章 当我们在eclipse中开发android程序的时候.往往须要看源码(可能是出于好奇,可能是读源码习惯),那么怎样查看Android源码呢? 比方以下这样的情况 图1 如果我们想 ...

  7. 有关AngularJS请求Web API资源的思路

    页面部分大致如下: <body ng-app="productManagement"> ... <div ng-include="'app/produc ...

  8. Delphi 19种反调试检测法

    //使用IsDebuggerPresent这个API来检测是否被调试function FD_IsDebuggerPresent(): Boolean;beginif IsDebuggerPresent ...

  9. delphi services允许跨域访问

    delphi services允许跨域访问 unit WebModuleUnit1; procedure TWebModule1.WebModule1DefaultHandlerAction(Send ...

  10. C/C++中const关键字

    http://blog.csdn.net/xdrt81y/article/details/24333335 今天在做一个趋势笔试题的时候,才让我有了系统把const关键字好好总结一下的冲动,因为这个关 ...