LeetCode 113 Path Sum II (DFS)

原创 2016年08月30日 09:15:56

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;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【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
  • 75

[Leetcode 113, Medium] Path sum II

[Leetcode 113, Medium] Path sum II

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

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

113. Path Sum II  Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium Given...

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

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

112. Path Sum My Submissions Question Total Accepted: 85016 Total Submissions: 278387 Difficulty...

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 ...
  • fly_yr
  • fly_yr
  • 2015年10月20日 12:54
  • 731

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

LeetCode 113. Path Sum II(路径和)

原题网址:https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-l...
  • jmspan
  • jmspan
  • 2016年05月24日 00:19
  • 307

LeetCode 112&113 --Path Sum I & II;

Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that a...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 113 Path Sum II (DFS)
举报原因:
原因补充:

(最多只允许输入30个字)