-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path113-PathSum2.cs
39 lines (34 loc) · 1.15 KB
/
113-PathSum2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//-----------------------------------------------------------------------------
// Runtime: 292ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _113_PathSum2
{
public IList<IList<int>> PathSum(TreeNode root, int sum)
{
var current = new List<int>();
var results = new List<IList<int>>();
PathSum(root, sum, current, results);
return results;
}
public void PathSum(TreeNode root, int sum, IList<int> current, IList<IList<int>> results)
{
if (root == null) return;
sum -= root.val;
current.Add(root.val);
if (root.left == null && root.right == null && sum == 0)
{
results.Add(new List<int>(current));
current.RemoveAt(current.Count - 1);
return;
}
PathSum(root.left, sum, current, results);
PathSum(root.right, sum, current, results);
current.RemoveAt(current.Count - 1);
}
}
}