-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumRootToLeafNumbers.cs
More file actions
41 lines (32 loc) · 963 Bytes
/
SumRootToLeafNumbers.cs
File metadata and controls
41 lines (32 loc) · 963 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 System;
using System.Collections.Generic;
using System.Text;
namespace CodeForecs
{
//https://leetcode.com/problems/sum-root-to-leaf-numbers/
class SumRootToLeafNumbers
{
public int SumNumbers(TreeNode root)
{
int result = 0;
SumNumbersHelper(root, ref result, "");
return result;
}
public void SumNumbersHelper(TreeNode root, ref int result, string pathString)
{
if (root == null)
{
return;
}
if (root.left == null && root.right == null)
{
pathString += root.val.ToString();
result += Convert.ToInt32(pathString);
return;
}
pathString += root.val.ToString();
SumNumbersHelper(root.left, ref result, pathString);
SumNumbersHelper(root.right, ref result, pathString);
}
}
}