关闭

LeetCode 113 Path Sum II (DFS)

标签: leetcodeDFS
395人阅读 评论(0) 收藏 举报
分类:

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


题目链接:https://leetcode.com/problems/path-sum-ii/


题目分析:直接DFS即可,到叶子节点时判断
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    public void DFS(int curSum, int sum, TreeNode root, List<Integer> cur, List<List<Integer>> ans) {
        if(root == null) {
            return;
        }
        if(root != null && (root.left == null && root.right == null)) {
            if(curSum + root.val == sum) {
                cur.add(root.val);
                ans.add(new ArrayList<Integer>(cur));
                cur.remove(cur.size() - 1);
            }
            return;
        }
        cur.add(root.val);
        DFS(curSum + root.val, sum, root.left, cur, ans);
        DFS(curSum + root.val, sum, root.right, cur, ans);
        cur.remove(cur.size() - 1);
    }
    
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> cur = new ArrayList<>();
        DFS(0, sum, root, cur, ans);
        return ans;
    }
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【LeetCode-面试算法经典-Java实现】【113-Path Sum II(路径和)】

【113-Path Sum II(路径和II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree and a sum, find al...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-12 06:23
  • 2483

【LeetCode-面试算法经典-Java实现】【112-Path Sum(路径和)】

【112-Path Sum(路径和)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree and a sum, determine if...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-11 07:40
  • 2787

Leetcode 454. 4Sum II 四数之和2 解题报告

1 解题思想首先,这是一道远古之前的题的进化版: Leetcode #18 4Sum 四数之和 解题小节+K-Sum思想 但是我不想说那个题了,因为我也记不住了。。这道题意思就是ABCD四个数组,长...
  • MebiuW
  • MebiuW
  • 2016-11-16 22:45
  • 2754

【LeetCode笔记】113. Path Sum II DFS搜索+记录节点

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the...
  • macidoo
  • macidoo
  • 2017-04-15 16:26
  • 106

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II  Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium Given...
  • Jin_Kwok
  • Jin_Kwok
  • 2016-05-09 22:25
  • 831

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...
  • u014568921
  • u014568921
  • 2016-03-11 14:14
  • 285

[Leetcode 113, Medium] Path sum II

[Leetcode 113, Medium] Path sum II
  • u012212811
  • u012212811
  • 2015-08-04 01:41
  • 221

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...
  • Dream_angel_Z
  • Dream_angel_Z
  • 2015-06-12 16:33
  • 1168

LeetCode 113: Path Sum II

Path Sum IIGiven a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals t...
  • yongyuandeie
  • yongyuandeie
  • 2015-10-28 15:18
  • 229

<LeetCode OJ> 112/113. Path Sum(I / II)

112. Path Sum My Submissions Question Total Accepted: 85016 Total Submissions: 278387 Difficulty...
  • EbowTang
  • EbowTang
  • 2016-01-11 11:31
  • 714
    个人资料
    • 访问:708360次
    • 积分:14952
    • 等级:
    • 排名:第910名
    • 原创:815篇
    • 转载:7篇
    • 译文:0篇
    • 评论:122条
    博客专栏