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

SOLUTION 1:

使用递归解决,先把下一个可能要加的节点加入到path中,再使用递归依次计算即可。

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public List<List<Integer>> pathSum(TreeNode root, int sum) {
12         List<List<Integer>> ret = new ArrayList<List<Integer>>();
13
14         List<Integer> path = new ArrayList<Integer>();
15         if (root == null) {
16             return ret;
17         }
18
19         path.add(root.val);
20         sum -= root.val;
21
22         dfs(root, sum, path, ret);
23
24         return ret;
25     }
26
27     public void dfs(TreeNode root, int sum, List<Integer> path, List<List<Integer>> ret) {
28         if (root == null) {
29             return;
30         }
31
32         if (sum == 0 && root.left == null && root.right == null) {
33             ret.add(new ArrayList<Integer>(path));
34             return;
35         }
36
37         if (root.left != null) {
38             path.add(root.left.val);
39             dfs(root.left, sum - root.left.val, path, ret);
40             path.remove(path.size() - 1);
41         }
42
43         if (root.right != null) {
44             path.add(root.right.val);
45             dfs(root.right, sum - root.right.val, path, ret);
46             path.remove(path.size() - 1);
47         }
48     }
49 }

SOLUTION 2:

使用递归解决,如果只考虑加入当前节点,会更加简单易理解。递归的base case就是:

1. 当null的时候返回。

2. 当前节点是叶子 并且sum与root的值相同,则增加一个可能的解。

3. 如果没有解,将sum 减掉当前root的值,并且向左树,右树递归即可。

 1 // SOLUTION 2
 2     public List<List<Integer>> pathSum(TreeNode root, int sum) {
 3         List<List<Integer>> ret = new ArrayList<List<Integer>>();
 4
 5         List<Integer> path = new ArrayList<Integer>();
 6         if (root == null) {
 7             return ret;
 8         }
 9
10         dfs2(root, sum, path, ret);
11
12         return ret;
13     }
14
15     public void dfs2(TreeNode root, int sum, List<Integer> path, List<List<Integer>> ret) {
16         if (root == null) {
17             return;
18         }
19
20         path.add(root.val);
21         sum -= root.val;
22         if (sum == 0 && root.left == null && root.right == null) {
23             ret.add(new ArrayList<Integer>(path));
24         } else {
25             dfs2(root.left, sum, path, ret);
26             dfs2(root.right, sum, path, ret);
27         }
28
29         path.remove(path.size() - 1);
30     }

时间: 2024-10-23 17:16:38

LeetCode: Path Sum II 解题报告的相关文章

LeetCode: Combination Sum II 解题报告

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note:All numbers (including ta

【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 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] ] [解析] DFS,递归实现. /** *

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 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] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

LeetCode: Path Sum II [113]

[题目] 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] ] [题意] 判断二叉树中是否存在一条从根到

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

[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 sum. For example:Given the below binary tree andsum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题意:给定一数,在树中找出所有路径和等于该数的情况.方

[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 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的延伸,只不过

LeetCode: Word Break II 解题报告

Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat",