-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0113-Path-sum-II.cs
More file actions
41 lines (31 loc) · 1013 Bytes
/
0113-Path-sum-II.cs
File metadata and controls
41 lines (31 loc) · 1013 Bytes
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
40
41
using Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0113.Path_sum_II
{
public class _0113_Path_sum_II
{
private IList<IList<int>> _res = new List<IList<int>>();
private List<int> _curr = new List<int>();
public IList<IList<int>> PathSum(TreeNode root, int targetSum)
{
if (root == null) return _res;
travel(root, targetSum);
return _res;
}
private void travel(TreeNode node, int sum)
{
if (node == null) return;
_curr.Add(node.val);
if (node.val == sum && node.left == null && node.right == null)
_res.Add(new List<int>(_curr));
if (node.left != null)
travel(node.left, sum - node.val);
if (node.right != null)
travel(node.right, sum - node.val);
// delete last item.
_curr.RemoveAt(_curr.Count - 1);
}
}
}